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
It's true that p
(&Vertex{}
) has type *Vertex
and that c
(Vertex{}
) has type Vertex
.
However, I don't believe that statement really answers the question of why one would choose one over the other. It'd be kind of like answering the question "why use planes over cars" with something like "planes are winged, cars aren't." (Obviously we all know what wings are, but you get the point).
But it also wouldn't be very helpful to simply say "go learn about pointers" (though I think it is a really good idea to so nonetheless).
How you choose basically boils down to the following.
Realize that &Vertex{}
and Vertex{}
are fundamentally initialized in the same way.
What makes one more useful and performant than the other is determined by how they are used in the program.
"Do I want a pointer to the struct (p
), or just the struct (c
)?"
&
operator (e.g. &c
); you can dereference a pointer to get the value using *
(e.g. *p
)
*p
or &c
Vertex{}
or &Vertex{}
?
Vertex
given in your example, I'd choose Vertex{}
to a get a simple value object.Vertex
in the example is pretty small in terms of size. Copying is cheap.Vertex
doesn't really contain anything worth mutating (just return/create a new Vertex
when needed).&Struct{}
Struct
has a member caching some state that needs to be changed inside the original object itself.Struct
is huge, and you've done enough profiling to determine that the cost of copying is significant.
struct
s small, it's just good practice. v := Struct{}
v2 := v // Copy happens
v := &Struct{}
v2 := v // Only a pointer is copied
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.