Logo

0x5a.live

for different kinds of informations and explorations.

GitHub - emvi/null: Nullable Go types that can be marshalled/unmarshalled to/from JSON.

Nullable Go types that can be marshalled/unmarshalled to/from JSON. - emvi/null

Visit SiteGitHub - emvi/null: Nullable Go types that can be marshalled/unmarshalled to/from JSON.

GitHub - emvi/null: Nullable Go types that can be marshalled/unmarshalled to/from JSON.

Nullable Go types that can be marshalled/unmarshalled to/from JSON. - emvi/null

Powered by 0x5a.live ๐Ÿ’—

Nullable Go types

Go Reference CircleCI Go Report Card

Description

This package provides nullable Go types for bool, float64, int64, int32, 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

Note that from 1.3 on "null" requires Go version 1.13 or newer.

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

GoLang Resources

are all listed below.

Resources

listed to get explored on!!

Made with โค๏ธ

to provide different kinds of informations and resources.