Golang pointers

后端 未结 5 719
眼角桃花
眼角桃花 2020-12-23 17:44

I am currently learning to program with Go language. I am having some difficulties understanding Go pointers (and my C/C++ is far away now...). In the Tour of Go #52 (http:/

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-23 18:14

    The difference is pass-by-referenve vs pass-by-value.

    In func f(v Vertex) the argument is copied into parameter v. In func f(v *Vertex) a pointer to an existing Vertex instance is passed.

    When using methods, some of the dereferencing can be done for you, so you can have a method func (v *Vertex) f() and call it without taking a pointer first: v := Vertex{...}; v.f(). This is just a grain of syntax sugar, AFAIK.

提交回复
热议问题