When building a simple object in go, what's the difference between these alternatives?
func NewGender(value string) Gender {
return Gender{strings.TrimSpace(value)}
}
func NewGender(value string) *Gender {
return &Gender{strings.TrimSpace(value)}
}
For many Go developers, the systematic use of pointers to share structs instead of the copy itself seems the best option in terms of performance.
“ Pointers are used for efficiency because everything in Go is passed by value so they let us pass an address where data is held instead of passing the data's value, to avoid unintentionally changing data, and so we can access an actual value in another function and not just a copy of it when we want to mutate it .”
The short answer to why you “have to” return a pointer is that you clearly don't have to return anything. You are not returning pointer (but an int ) and you are not using the returned value for anything. You can change function's return type to void and remove the line with return and it will work just as well.
The * and & operators In Go a pointer is represented using the * (asterisk) character followed by the type of the stored value. In the zero function xPtr is a pointer to an int . * is also used to “dereference” pointer variables. Dereferencing a pointer gives us access to the value the pointer points to.
The question is a really broad one, and it depends heavily on the rest of your API. So here are just some things you might need to consider when choosing one over another (in no particular order):
Unnecessary pointers lead to more work for the GC. You might win some time by returning a pointer (one word) rather than a struct (zero to many words), but some of that time might later be consumed by the GC scan.
Pointers can also signal presence or absence of a thing, although it's generally better to use comma-ok idiom for that, or return an error.
Speaking of errors, if you return nil
with an error, the chance that the error will be ignored is smaller (because nil pointer dereference will result in panic). Then again, I don't think people who ignore errors are something you should really take into account.
If you need some form of tracking (for example, a possibility to inspect all instances ever created by your constructor), you have to return a pointer, so that all changes to the value would be visible to the inspector.
If most of the type's methods have a pointer receiver, or most functions in the API accept pointers to the type rather than values, it's more ergonomic to return a pointer.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With