Go Pattern: Is Zero Value a Change ?
As we know in Go, variables declared without an explicit initial value are assigned their zero value.
|
|
Here a
is an empty string. This is ok. Sane defaults.
The problem arises when we want to check if a field’s value has changed to it’s zero
value. The solution to this problem is pretty simple.
|
|
Making the field a pointer to string helps. To keep a field unchanged we simply don’t assign to it. As in the above example, it will be set to zero value of pointer which is nil.
|
|
This is trivial but comes up quite often.