Documentation ¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type String ¶
String represents a string that may be null. String implements the Scanner interface so it can be used as a scan destination. String implements the Marshaller interface so it can be used to read and write null values.
func NewString ¶
Example ¶
package main import ( "fmt" "github.com/tomwright/null" ) func main() { a := null.NewString("asd") b := null.NewString("") fmt.Printf("a: valid: `%v`, string: `%s`\n", a.Valid, a.String) fmt.Printf("b: valid: `%v`, string: `%s`\n", b.Valid, b.String) }
Output: a: valid: `true`, string: `asd` b: valid: `false`, string: ``
func (String) MarshalJSON ¶
Example ¶
package main import ( "encoding/json" "fmt" "github.com/tomwright/null" ) func main() { a := null.NewString("asd") b := null.NewString("") aBytes, _ := json.Marshal(a) bBytes, _ := json.Marshal(b) fmt.Printf("a: %s\n", string(aBytes)) fmt.Printf("b: %s", string(bBytes)) }
Output: a: "asd" b: null
func (*String) UnmarshalJSON ¶
Example ¶
package main import ( "encoding/json" "fmt" "github.com/tomwright/null" ) func main() { var ( a null.String b null.String c null.String d null.String ) json.Unmarshal([]byte(`"asd"`), &a) json.Unmarshal([]byte(`null`), &b) json.Unmarshal([]byte(`""`), &c) json.Unmarshal(nil, &d) fmt.Printf("a: valid: `%v`, string: `%s`\n", a.Valid, a.String) fmt.Printf("b: valid: `%v`, string: `%s`\n", b.Valid, b.String) fmt.Printf("c: valid: `%v`, string: `%s`\n", c.Valid, c.String) fmt.Printf("d: valid: `%v`, string: `%s`\n", d.Valid, d.String) }
Output: a: valid: `true`, string: `asd` b: valid: `false`, string: `` c: valid: `false`, string: `` d: valid: `false`, string: ``
type Time ¶
Time implements the Scanner interface so it can be used as a scan destination. Time implements the Marshaller interface so it can be used to read and write null values.
func NewTime ¶
Example ¶
package main import ( "fmt" "github.com/tomwright/null" "time" ) func main() { a := null.NewTime(time.Time{}) b := null.NewTime(time.Date(2019, 01, 01, 12, 00, 00, 0, time.UTC)) fmt.Printf("a: valid: `%v`, time: `%s`\n", a.Valid, a.Time.Format(time.RFC3339)) fmt.Printf("b: valid: `%v`, time: `%s`\n", b.Valid, b.Time.Format(time.RFC3339)) }
Output: a: valid: `false`, time: `0001-01-01T00:00:00Z` b: valid: `true`, time: `2019-01-01T12:00:00Z`
func (Time) MarshalJSON ¶
MarshalJSON returns either a marshal'd time.Time if it was valid, or a marshal'd NULL value if it was not valid.
func (Time) TimeOrZero ¶
TimeOrZero returns either a valid time.Time, or a zero-value time.Time object.
func (*Time) UnmarshalJSON ¶
Example ¶
package main import ( "encoding/json" "fmt" "github.com/tomwright/null" "time" ) func main() { var ( a null.Time b null.Time c null.Time ) json.Unmarshal([]byte(`"2019-01-01T12:00:00Z"`), &a) json.Unmarshal([]byte(`null`), &b) json.Unmarshal([]byte(`""`), &c) fmt.Printf("a: valid: `%v`, time: `%s`\n", a.Valid, a.Time.Format(time.RFC3339)) fmt.Printf("b: valid: `%v`, time: `%s`\n", b.Valid, b.Time.Format(time.RFC3339)) fmt.Printf("c: valid: `%v`, time: `%s`\n", c.Valid, c.Time.Format(time.RFC3339)) }
Output: a: valid: `true`, time: `2019-01-01T12:00:00Z` b: valid: `false`, time: `0001-01-01T00:00:00Z` c: valid: `false`, time: `0001-01-01T00:00:00Z`
Click to show internal directories.
Click to hide internal directories.