如何从函数中返回动态值

huangapple go评论85阅读模式
英文:

How to return dynamic values from a function

问题

如何实现这样的函数?<What_to_use_here>

func callFunc(type string) <What_to_use_here> {
    if type == "string" {
        return "I am a string"
    } else if type == "integer" {
        return 1
    }
    return nil
}

type Sample struct {
    value_int int
    value_str string
}

var s Sample
s.value_int = callFunc("integer")
s.value_str = callFunc("string")

我需要将来自callFunc的值分配给指定的类型,如示例所示。所以我认为返回interface类型不起作用。需要帮助。

英文:

How to implement a function like this? &lt;What_to_use_here&gt; ?

func callfunc(type string) &lt;What_to_use_here&gt; {
    if type == &quot;string&quot;{
        return &quot;I am a string&quot;
     } else if type == &quot;integer&quot;{
       return 1
     }
    return nil
}

type Sample struct {
   value_int int
   value_str string
}

type Sample s
s.value_int = callFunc(&quot;integer&quot;)
s.value_str = callFunc(&quot;string&quot;)

I need to assign the value coming from the callFunc to a specified type as shown. So I think returning interface will not work. Need help.

答案1

得分: 1

type Sample struct {
	value_int int
	value_str string
}

func callFunc(typ string) interface{} {
	if typ == "string" {
		return "我是一个字符串"
	} else if typ == "integer" {
		return 1
	}
	return nil
}

func main() {
	var s Sample
	s.value_int = callFunc("integer").(int)
	s.value_str = callFunc("string").(string)
	fmt.Println(s)
}

https://play.golang.org/p/QEPVMvAhkMQ

英文:
type Sample struct {
	value_int int
	value_str string
}

func callFunc(typ string) interface{} {
	if typ == &quot;string&quot; {
		return &quot;I am a string&quot;
	} else if typ == &quot;integer&quot; {
		return 1
	}
	return nil
}

func main() {
	var s Sample
	s.value_int = callFunc(&quot;integer&quot;).(int)
	s.value_str = callFunc(&quot;string&quot;).(string)
	fmt.Println(s)
}

https://play.golang.org/p/QEPVMvAhkMQ

答案2

得分: 1

你可以使用interface来从Go语言函数中返回动态值,以下是一个简单的示例程序:

package main

import (
	"fmt"
)

func main() {
	data := callfunc("string")
	fmt.Println(data)
	data2 := callfunc("integer")
	fmt.Println(data2)
}

func callfunc(str string) interface{} {
	if str == "string" {
		return "I am a string"
	} else if str == "integer" {
		return 1
	}
	return nil
}

输出结果:

I am a string
1
英文:

You can make use of interface to return dynamic values from go lang function,here is simple program for the same:

package main

import (
	&quot;fmt&quot;
)

func main() {
	data := callfunc(&quot;string&quot;)
	fmt.Println(data)
	data2 := callfunc(&quot;integer&quot;)
	fmt.Println(data2)
}

func callfunc(str string) interface{} {
	if str == &quot;string&quot; {
		return &quot;I am a string&quot;
	} else if str == &quot;integer&quot; {
		return 1
	}
	return nil
}

Output:

I am a string
1

huangapple
  • 本文由 发表于 2021年7月16日 18:49:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/68407764.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定