小编Tom*_*omy的帖子

为什么我应该在结构上使用 & 符号?

在 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

14
推荐指数
2
解决办法
4012
查看次数

标签 统计

go ×1