这可能不是一个新问题,但我在任何地方都找不到答案.
使用这段代码,在循环中包含函数调用的行上,:=
或=
运算符都不起作用.
我有一个用例,其中我需要在for循环外部声明一次大型数组,并在函数中更新,然后传回.但该函数还返回另一个变量,该变量每次都不同,并在该循环中使用.
Go操场链接:1
import "fmt"
func someFunc(names []string) (int, []string) {
foo := 35 // Just for the example
names = append(names, "Bob")
return foo, names
}
func main() {
names := []string{"Fred", "Mary"}
for i := 0; i < 10; i++ {
newVariable, names := someFunc(names) // This line is the problem
fmt.Println(newVariable)
}
}
我如何重构它以使其按预期工作?