June 2017
Intermediate to advanced
400 pages
10h 32m
English
Switching from a value to a pointer is a quick way to express null values when marshaling and unmarshaling. It can be a bit unclear on setting these values as you can't assign them directly to a pointer -- *a := 1, but otherwise it's a flexible way of dealing with it.
This recipe also demonstrated an alternative method using the sql.NullInt64 type. This is normally used with SQL and valid is set if anything other than Null is returned, otherwise it sets Null. We added a MarshalJSON and UnmarshallJSON method to allow this type to interact with the JSON package and we chose to use a pointer so that omitempty would continue to work as expected.
Read now
Unlock full access