We can create structs in golang this way. Examples below: What are differences between these two?
// Usual way
type Employee struct {
firstName string `json:"name"`
salary int `json:"salary"`
fullTime bool `json:"fullTime"`
projects []Project `json:"projects"`
}
// Un-usal way with pointers
type Employee struct {
firstName *string `json:"name"`
salary *int `json:"salary"`
fullTime *bool `json:"fullTime"`
projects *[]Project `json:"projects"`
}
Are there any trade-offs like memory?
Update:
Assume below function:
// this function consumes MORE memory
func printEmployeeWithoutPointer(employee Employee) {
// print here
}
// this function consumes LESS memory
func printEmployeeWithPointer(employee *Employee) {
// print here
}
We use pointers to share data, but that doesn't always mean it is more memory efficient or more performant. Go is extremely good and fast at copying data.
When it comes to structs a common reason for using pointers is that pointers can have
nil
values, where primitives can't. If you need a struct with optionals field, you'd use pointersIf you are deserialising JSON then you could omit fields using
omitempty
. Here fullTime is optionalPerformance when using JSON
If you are deserializing JSON into pointers in the hopes of saving memory, you won't. From a JSON point of view each item is unique, so there is no sharing of data. You will use more memory, because each value now has to store a value and a pointer to the value. And it will be slower because you will need to dereference pointers the whole time
FYI, additional reading https://github.com/golang/go/wiki/CodeReviewComments#pass-values .
Pass Values Don't pass pointers as function arguments just to save a few bytes. If a function refers to its argument x only as *x throughout, then the argument shouldn't be a pointer. Common instances of this include passing a pointer to a string (*string) or a pointer to an interface value (*io.Reader). In both cases the value itself is a fixed size and can be passed directly. This advice does not apply to large structs, or even small structs that might grow.
Right, there's a number of things to consider. First up: let's start with the obvious syntax error in your pointer example:
So I've moved the asterisk to the type, and I've captialized the fields. The
encoding/json
package uses reflection to set the values of the fields, so they need to be exported.Seeing as you're using json tags, let's start with the simple things:
When I'm unmarshalling a message that has no
bar
value, theBar
field will just be an empty string. That makes it kind of hard to work out whether or not the field was sent or not. Especially when dealing with integers: how do I tell the difference between a field that wasn't sent vs a field that was sent with a value of 0?Using a pointer field, and specify
omitempty
allows you to do that. If the field wasn't specified in the JSON data, then the field in your struct will benil
, if not: it'll point to an integer of value 0.Of course, having to check for pointers being nil can be tedious, it makes code more error-prone, and so you only need to do so if there's an actual reason why you'd want to differentiate between a field not being set, and a zero value.
pitfalls
Pointers allow you to change values of what they point to
Let's move on to the risks pointers inherently bring with them. Assuming your
Employee
struct with pointer fields, and a type calledEmployeeV
that is the same but with value fields, consider these functions:Now this function is only going to work half of the time. You're calling
SetName
on a value receiver. IfFirstname
is nil, then you're going to set the pointer on a copy of your original variable, and your variable will not reflect the change you made in the function. IfFirstname
was set, however, the copy will point to the same string as your original variable, and the value that pointer points to will get updated. That's bad.Implement the same function on
EmployeeV
, however:And it simply won't ever work. You'll always update a copy, and the changes won't affect the variable on which you call the
SetName
function. For that reason, the idiomatic way, in go, to do something like this would be:So we're changing the method to use a pointer receiver.
Data races
As always: if you're using pointers, you're essentially allowing code to manipulate the memory something points to directly. Given how golang is a language that is known to facilitate concurrency, and accessing the same bit of memory means you're at risk of creating data-races:
This
Firstname
field is accessed in a lot of different routines. What will be its eventual value? Do you even know? The golang race detector will most likely flag this code as a potential data race.In terms of memory use: individual fields like ints or bools really aren't the thing you ought to be worried about. If you're passing around a sizeable struct, and you know it's safe, then it's probably a good idea to pass around a pointer to said struct. Then again, accessing values through a pointer rather than accessing them directly isn't free: indirection adds a small overhead.
i recommend to read this post a link