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条)

报告相同问题?

悬赏问题

  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条