Created
November 21, 2019 23:05
-
-
Save alfg/3325bbb23f973a4c802e07fbccdbe51a to your computer and use it in GitHub Desktop.
Null types golang
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// NullString is an alias for sql.NullString data type | |
type NullString struct { | |
sql.NullString | |
} | |
// MarshalJSON for NullString | |
func (ns *NullString) MarshalJSON() ([]byte, error) { | |
if !ns.Valid { | |
return []byte("null"), nil | |
} | |
return json.Marshal(ns.String) | |
} | |
// NullInt64 is an alias for sql.NullInt64 data type | |
type NullInt64 struct { | |
sql.NullInt64 | |
} | |
// MarshalJSON for NullInt64 | |
func (ni *NullInt64) MarshalJSON() ([]byte, error) { | |
if !ni.Valid { | |
return []byte("null"), nil | |
} | |
return json.Marshal(ni.Int64) | |
} | |
// NullFloat64 is an alias for sql.NullFloat64 data type | |
type NullFloat64 struct { | |
sql.NullFloat64 | |
} | |
// MarshalJSON for NullFloat64 | |
func (nf *NullFloat64) MarshalJSON() ([]byte, error) { | |
if !nf.Valid { | |
return []byte("null"), nil | |
} | |
return json.Marshal(nf.Float64) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment