How it works...

Switching from a value to a pointer is a quick way to express null values when marshaling and unmarshaling. Setting these values can be a bit tricky, 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 method and an UnmarshalJSON 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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.217.187.19