repositorypackage
1.0.1
Repository: https://github.com/r-fujiyama/nulltype.git
Documentation: pkg.go.dev
# README
nulltype
Installation
$ go get -u github.com/r-fujiyama/nulltype
Example
import (
"bytes"
"encoding/json"
"fmt"
"strings"
"github.com/r-fujiyama/nulltype"
)
func main() {
// JSON Encode
s1 := nulltype.String{String: "foo", Valid: true}
var buf bytes.Buffer
_ = json.NewEncoder(&buf).Encode(s1)
fmt.Print(buf.String()) // "foo"
buf.Reset()
s1 = nulltype.String{String: "", Valid: true}
_ = json.NewEncoder(&buf).Encode(s1)
fmt.Print(buf.String()) // ""
buf.Reset()
s1 = nulltype.String{String: "", Valid: false}
_ = json.NewEncoder(&buf).Encode(s1)
fmt.Print(buf.String()) // null
// JSON Decode
s2 := nulltype.String{}
_ = json.NewDecoder(strings.NewReader(`"foo"`)).Decode(&s2)
fmt.Printf("%#v\n", s2) // nulltype.String{String:"foo", Valid:true}
_ = json.NewDecoder(strings.NewReader(`""`)).Decode(&s2)
fmt.Printf("%#v\n", s2) // nulltype.String{String:"", Valid:true}
_ = json.NewDecoder(strings.NewReader("null")).Decode(&s2)
fmt.Printf("%#v\n", s2) // nulltype.String{String:"", Valid:false}
}