go - 误解:= in Go的用法

标签 go

我正在阅读 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/

相关文章:

go - 如何永久删除 GORM 中的关联

types - 为什么我可以键入别名函数并在不强制转换的情况下使用它们?

go - 在 golang 中 slice unicode/ascii 字符串?

go - 如何从 grpc-server 流式传输结果以及如何从 goroutines grpc server-stream 返回结果以将结果传递给 api

go - 检测僵尸子进程

json - 在 Go 中解码复杂的 json

go找不到sdl

http - 如何从 bytes.Buffer 中多次读取?

go - 如何调用 TCPConn.SetLinger()?

json - Golang 日期时间结构