在 gotour 中,有一个部分:struct literals。
package main
import "fmt"
type Vertex struct {
X, Y int
}
var (
v1 = Vertex{1, 2} // has type Vertex
v2 = Vertex{X: 1} // Y:0 is implicit
v3 = Vertex{} // X:0 and Y:0
p = &Vertex{1, 2} // has type *Vertex
)
func main() {
fmt.Println(v1, p, v2, v3)
}
Run Code Online (Sandbox Code Playgroud)
带有 & 符号的结构体和不带符号的结构体有什么区别?我知道带有 & 符号的那些指向相同的引用,但我为什么要在常规引用上使用它们呢?
var p = &Vertex{} // why should I use this
var c = Vertex{} // over this
Run Code Online (Sandbox Code Playgroud) go ×1