dongyigua4468 2018-02-03 21:03
浏览 48
已采纳

继续-何时使用“&MyStruct {1,2}”语法实例化结构并获取指向该结构的指针会有用吗?

I have been following the tour of the Go programming language to get familiar with the language, and one feature of the language left me wondering.

In the step about Struct Literals, they explain that you can instantiate a structure by multiple ways:

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
)

I have no problem understanding how the three first ways are working and when they can be useful, but I cannot figure out any use-case for the last solution p = &Vertex{1, 2}.

Indeed, I cannot think of any case where one would have to instantiate a *Vertex without having already a variable of type Vertex available and used in your code:

func modifyingVertexes(myVertex *Vertex) {
    myVertex.X = 42;
}

func main() {
    myVertex := Vertex{1, 2}

    modifyingVertexes(&myVertex)

    fmt.Println(myVertex.X)
}

I could see a use if the following was possible:

func modifyingVertexes(myVertex *Vertex) {
    myVertex.X = 42;
}

func main() {
    modifyingVertexes(&Vertex{1, 2})

    fmt.Println(???.X) // Accessing the vertex initialized in the modifyingVertexes func call
}

But since I don't think it's possible, I really have no idea on how it could be used?

Thanks!

  • 写回答

2条回答 默认 最新

  • duangangpin078794 2018-02-03 21:35
    关注

    This is a very common Go idiom, returning the address of an initialized struct variable.

    package main
    
    import "fmt"
    
    type Vertex struct {
        X, Y int
    }
    
    func NewVertex(x, y int) *Vertex {
        return &Vertex{X: x, Y: y}
    }
    
    func main() {
        v := NewVertex(1, 2)
        fmt.Println(*v)
    }
    

    Playground: https://play.golang.org/p/UGOk7TbjC2a

    Output:

    {1 2}
    

    It's also a very common idiom for hiding unexported (private) struct fields.

    package main
    
    import "fmt"
    
    type Vertex struct {
        x, y int
    }
    
    func NewVertex(x, y int) *Vertex {
        return &Vertex{x: x, y: y}
    }
    
    func (v *Vertex) Clone() *Vertex {
        return &Vertex{x: v.x, y: v.y}
    }
    
    func main() {
        v := NewVertex(1, 2)
        fmt.Println(*v)
    
        w := v.Clone()
        fmt.Println(*w)
    }
    

    Playground: https://play.golang.org/p/ScIqOIaYGPn

    Output:

    {1 2}
    {1 2}
    

    In Go, all arguments are passed by value. Using a pointer for large structs is more efficient. A pointer is also necessary if you wish to change the value of a struct argument.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 labview程序设计
  • ¥15 为什么在配置Linux系统的时候执行脚本总是出现E: Failed to fetch http:L/cn.archive.ubuntu.com
  • ¥15 Cloudreve保存用户组存储空间大小时报错
  • ¥15 伪标签为什么不能作为弱监督语义分割的结果?
  • ¥15 编一个判断一个区间范围内的数字的个位数的立方和是否等于其本身的程序在输入第1组数据后卡住了(语言-c语言)
  • ¥15 游戏盾如何溯源服务器真实ip?
  • ¥15 Mac版Fiddler Everywhere4.0.1提示强制更新
  • ¥15 android 集成sentry上报时报错。
  • ¥50 win10链接MySQL
  • ¥15 抖音看过的视频,缓存在哪个文件