The first item returned is a value, the second is a pointer. The pointer works much like a pointer in C or C++ only the value it points to is garbage collected like in C# or Java. Go offers something of a compromise between those two paradigms. The pointer is explicit and exposed, however it's still garbage collected. Here are a few somewhat obvious differences;
1) If you pass the value to a method or it is the receiver (which is often confusing) you will not be able to modify it's value, it will be a 'pass by value' like in the other languages where you are modifying a copy. Yes, even the method func (g Gender) ChangeGender()
will not change the gender of the object you call it on. https://play.golang.org/
2) You can only call methods defined for the type. For example if you have g *Gender
and you want to call the ChangeGender
method above you will not be able to without dereferencing your pointer, just like in C/C++. The opposite is true going in the opposite direction.
3) The alternative to passing by value is by reference, you probably know how that works but in case you don't I'll explain. If you have a method func (g *Gender) ModifyGender()
and an instance g := &Gender{}
then when calling ModifyGender
a pointer of the OS' word size will get pushed onto the stack as an argument rather than the value itself and the method will use that address to modify the memory there, persisting changes when control returns to the caller. This can vastly improve performance if you have method which work on objects that have a large memory foot print.
I'm not going to speak to performance. That should give you enough information to consider how it works in your application.
*Gender
to some other code, it can change it. Not so with theGender
. – Philana