Categorygithub.com/volatiletech/null

# README

null GoDoc Coverage

null is a library with reasonable options for dealing with nullable SQL and JSON values.

Types in null will only be considered null on null input, and will JSON encode to null.

All types implement sql.Scanner and driver.Valuer, so you can use this library in place of sql.NullXXX. All types also implement: encoding.TextMarshaler, encoding.TextUnmarshaler, json.Marshaler, json.Unmarshaler and sql.Scanner.


Installation

Null used to be versioned with gopkg.in, so once you upgrade to v8 and beyond please stop using gopkg.in and ensure you're using go modules.

go get github.com/volatiletech/null/v9

Usage

The following are all types supported in this package. All types will marshal to JSON null if Invalid or SQL source data is null.

TypeDescriptionNotes
null.JSONNullable []byteWill marshal to JSON null if Invalid. []byte{} input will not produce an Invalid JSON, but []byte(nil) will. This should be used for storing raw JSON in the database. Also has null.JSON.Marshal and null.JSON.Unmarshal helpers to marshal and unmarshal foreign objects.
null.BytesNullable []byte[]byte{} input will not produce an Invalid Bytes, but []byte(nil) will. This should be used for storing binary data (bytes in PSQL for example) in the database.
null.StringNullable string
null.ByteNullable byte
null.BoolNullable bool
null.TimeNullable `time.TimeMarshals to JSON null if SQL source data is null. Uses time.Time's marshaler.
null.Float32Nullable float32
null.Float64Nullable float64
null.IntNullable int
null.Int8Nullable int8
null.Int16Nullable int16
null.Int32Nullable int32
null.Int64Nullable int64
null.UintNullable uint
null.Uint8Nullable uint8
null.Uint16Nullable uint16
null.Uint32Nullable uint32
null.Uint64Nullable uint64

Bugs

json's ",omitempty" struct tag does not work correctly right now. It will never omit a null or empty String. This might be fixed eventually.

License

BSD-3 (See License file)

# Packages

No description provided by the author