我正在阅读 this doc并看到以下片段:
The := syntax is shorthand for declaring and initializing a variable, e.g. for var f string = "short" in this case.
f := "short"
fmt.Println(f)
关键是:它只适用于字符串吗?或者它是否足够动态以理解它应该存储什么数据类型?
还有:这不是和 var f = "short"
一样吗?
最佳答案
当然,它会推断出右侧表达式返回的明显类型。
specification给出这些例子:
i, j := 0, 10
f := func() int { return 7 }
ch := make(chan int)
r, w := os.Pipe(fd) // os.Pipe() returns two values
_, y, _ := coord(p) // coord() returns three values; only interested in y coordinate
请注意,它不是动态的:一切都在编译时发生,类型由正确的部分表达式给出。
关于go - 误解:= in Go的用法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27530523/