Skip to content

Instantly share code, notes, and snippets.

@jheth
Created March 22, 2018 13:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jheth/c755e128a015456797e13c9d6197e7ec to your computer and use it in GitHub Desktop.
Save jheth/c755e128a015456797e13c9d6197e7ec to your computer and use it in GitHub Desktop.
NullInt64 MarshalJSON
// NullInt64 is an alias for sql.NullInt64 data type
type NullInt64 sql.NullInt64
// MarshalJSON for NullInt64
func (ni *NullInt64) MarshalJSON() ([]byte, error) {
if !ni.Valid {
return []byte("null"), nil
}
return json.Marshal(ni.Int64)
}
// UnmarshalJSON for NullInt64
func (ni *NullInt64) UnmarshalJSON(b []byte) error {
err := json.Unmarshal(b, &ni.Int64)
ni.Valid = (err == nil)
return err
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment