
map有所不同,因为它是内置 类型
而不是函数。Go语言规范:索引表达式
map指定了访问a元素的2种形式。
使用函数,您将无法做到这一点。如果一个函数有2个返回值,则必须“期望”两个返回值或根本没有。
s, b := Hello() // Storing both of the return valuess2, _ := Hello() // Storing only the first_, b3 := Hello() // Storing only the second
您还可以选择不存储任何返回值:
Hello() // Just executing it, but storing none of the return values
注意:您也可以将两个返回值都分配给空白标识符,尽管它没有用(除了验证它确实有两个返回值):
_, _ = Hello() // Storing none of the return values; note the = instead of :=
您也可以在Go Playground上尝试这些。
辅助功能
如果您多次使用它,并且不想使用空白标识符,请创建一个放弃第二个返回值的帮助器函数:
func Hello2() string { s, _ := Hello() return s}现在,您可以执行以下 *** 作:
value := Hello2()fmt.Println(value)
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)