| 1 | ## null [](https://godoc.org/github.com/guregu/null) [](https://circleci.com/gh/guregu/null) |
| 2 | `import "gopkg.in/guregu/null.v3"` |
| 3 | |
| 4 | null is a library with reasonable options for dealing with nullable SQL and JSON values |
| 5 | |
| 6 | There are two packages: `null` and its subpackage `zero`. |
| 7 | |
| 8 | Types in `null` will only be considered null on null input, and will JSON encode to `null`. If you need zero and null be considered separate values, use these. |
| 9 | |
| 10 | Types in `zero` are treated like zero values in Go: blank string input will produce a null `zero.String`, and null Strings will JSON encode to `""`. Zero values of these types will be considered null to SQL. If you need zero and null treated the same, use these. |
| 11 | |
| 12 | 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`, and `json.Unmarshaler`. |
| 13 | |
| 14 | ### null package |
| 15 | |
| 16 | `import "gopkg.in/guregu/null.v3"` |
| 17 | |
| 18 | #### null.String |
| 19 | Nullable string. |
| 20 | |
| 21 | Marshals to JSON null if SQL source data is null. Zero (blank) input will not produce a null String. Can unmarshal from `sql.NullString` JSON input or string input. |
| 22 | |
| 23 | #### null.Int |
| 24 | Nullable int64. |
| 25 | |
| 26 | Marshals to JSON null if SQL source data is null. Zero input will not produce a null Int. Can unmarshal from `sql.NullInt64` JSON input. |
| 27 | |
| 28 | #### null.Float |
| 29 | Nullable float64. |
| 30 | |
| 31 | Marshals to JSON null if SQL source data is null. Zero input will not produce a null Float. Can unmarshal from `sql.NullFloat64` JSON input. |
| 32 | |
| 33 | #### null.Bool |
| 34 | Nullable bool. |
| 35 | |
| 36 | Marshals to JSON null if SQL source data is null. False input will not produce a null Bool. Can unmarshal from `sql.NullBool` JSON input. |
| 37 | |
| 38 | #### null.Time |
| 39 | |
| 40 | Marshals to JSON null if SQL source data is null. Uses `time.Time`'s marshaler. Can unmarshal from `pq.NullTime` and similar JSON input. |
| 41 | |
| 42 | ### zero package |
| 43 | |
| 44 | `import "gopkg.in/guregu/null.v3/zero"` |
| 45 | |
| 46 | #### zero.String |
| 47 | Nullable string. |
| 48 | |
| 49 | Will marshal to a blank string if null. Blank string input produces a null String. Null values and zero values are considered equivalent. Can unmarshal from `sql.NullString` JSON input. |
| 50 | |
| 51 | #### zero.Int |
| 52 | Nullable int64. |
| 53 | |
| 54 | Will marshal to 0 if null. 0 produces a null Int. Null values and zero values are considered equivalent. Can unmarshal from `sql.NullInt64` JSON input. |
| 55 | |
| 56 | #### zero.Float |
| 57 | Nullable float64. |
| 58 | |
| 59 | Will marshal to 0 if null. 0.0 produces a null Float. Null values and zero values are considered equivalent. Can unmarshal from `sql.NullFloat64` JSON input. |
| 60 | |
| 61 | #### zero.Bool |
| 62 | Nullable bool. |
| 63 | |
| 64 | Will marshal to false if null. `false` produces a null Float. Null values and zero values are considered equivalent. Can unmarshal from `sql.NullBool` JSON input. |
| 65 | |
| 66 | #### zero.Time |
| 67 | |
| 68 | Will marshal to the zero time if null. Uses `time.Time`'s marshaler. Can unmarshal from `pq.NullTime` and similar JSON input. |
| 69 | |
| 70 | |
| 71 | ### Bugs |
| 72 | `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](https://github.com/golang/go/issues/11939). |
| 73 | |
| 74 | ### License |
| 75 | BSD |