Why should I use the & sign on structs?

前端 未结 2 1089
借酒劲吻你
借酒劲吻你 2021-02-20 03:18

In the gotour, there is a section: struct literals.

package main

import \"fmt\"

type Vertex struct {
    X, Y int
}

var (
   v1 = Vertex{1, 2}  // has type Ve         


        
2条回答
  •  粉色の甜心
    2021-02-20 03:59

    The comments pretty much spell it out:

       v1 = Vertex{1, 2}  // has type Vertex
       p  = &Vertex{1, 2} // has type *Vertex
    

    As in many other languages, & takes the address of the identifier following it. This is useful when you want a pointer rather than a value.

    If you need to understand more about pointers in programming, you could start with this for go, or even the wikipedia page.

提交回复
热议问题