在Go中,我可以使用underscore忽略返回多个值的函数的返回值。例如:
res, _ := strconv.Atoi("64")假设我想要将第一个值直接用于另一个函数调用(在本例中,忽略错误检查最佳实践):
myArray := make([]int, strconv.Atoi("64"))编译器会抱怨我在单值上下文中使用了多值函数:
./array-test.go:11: multiple-value strconv.Atoi() in single-value context是否可以在单行中从返回值中“挑选并选择”,而不使用auxiliary functions
发布于 2016-05-03 05:50:11
唯一真正的方法是创建一些实用的“绕过”函数,因为这是Go,所以您必须为每个类型声明一个。
例如:
func noerrInt(i int, e err) int {
return i
}然后您可以执行以下操作:
myArray := make([]int, noerrInt(strconv.Atoi("64")))但实际上,这很糟糕,而且忽略了最佳实践。
https://stackoverflow.com/questions/36991947
复制相似问题