Skip to content
Nullable Go types that can be marshalled/unmarshalled to/from JSON.
Branch: master
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.circleci Updated link to project. Jan 16, 2019
CONTRIBUTING.md Added constructors and getters/setters, added contributing and better… Jul 9, 2018
LICENSE Initial commit Jul 4, 2018
README.md Fixed mistake in sample code. Apr 23, 2019
bool.go Added new nullable Time type. Mar 18, 2019
bool_test.go Added constructors and getters/setters, added contributing and better… Jul 9, 2018
doc.go Fixed linter errors. Feb 19, 2019
float64.go Added new nullable Time type. Mar 18, 2019
float64_test.go Added constructors and getters/setters, added contributing and better… Jul 9, 2018
int64.go Added new nullable Time type. Mar 18, 2019
int64_test.go
nullgopher.svg
string.go Added new nullable Time type. Mar 18, 2019
string_test.go Added constructors and getters/setters, added contributing and better… Jul 9, 2018
time.go Use SetValid instead of setting value directly in Time. Mar 18, 2019
time_test.go Added Scan and Value interface to time type. Mar 18, 2019

README.md

Nullable Go types

GoDoc CircleCI Go Report Card

Description

This package provides nullable Go types for bool, float64, int64, string and time.Time replacing sql.NullString, sql.NullInt64, ... that can be marshalled/unmarshalled to/from JSON.

Installation

To install "null", run go get within your project:

go get github.com/emvi/null

Usage

Here is a short example demonstrating the string type. The other types (int64, float64 and bool) work in the same manner.

package main

import (
    "encoding/json"
    "database/sql"
    "fmt"

    "github.com/emvi/null"
)

type NullableString struct {
    Value null.String `json:"value"`
}

func main() {
    str := NullableString{null.NewString("nullable string", true)}
    // or long version: str := NullableString{null.String{sql.NullString{String: "nullable string", Valid: true}}}
    
    data, _ := json.Marshal(str)
    fmt.Println(string(data)) // -> {"value": "nullable"}

    str.SetNil() // use str.SetValid("value") to set a value again
    data, _ = json.Marshal(str)
    fmt.Println(string(data)) // -> {"value": null}
}

Contribute

See CONTRIBUTING.md

License

MIT

You can’t perform that action at this time.