Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/emvi/null
Nullable Go types that can be marshalled/unmarshalled to/from JSON.
https://github.com/emvi/null
golang json marshal nullable nullbool nullfloat64 nullint64 nullstring nulltime sql unmarshal
Last synced: about 2 months ago
JSON representation
Nullable Go types that can be marshalled/unmarshalled to/from JSON.
- Host: GitHub
- URL: https://github.com/emvi/null
- Owner: emvi
- License: mit
- Created: 2018-07-04T21:18:45.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2021-11-09T16:04:18.000Z (about 3 years ago)
- Last Synced: 2024-07-31T20:48:18.307Z (4 months ago)
- Topics: golang, json, marshal, nullable, nullbool, nullfloat64, nullint64, nullstring, nulltime, sql, unmarshal
- Language: Go
- Size: 32.2 KB
- Stars: 35
- Watchers: 4
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
- awesome-go - null - Nullable Go types that can be marshalled/unmarshalled to/from JSON. (Data Structures and Algorithms / Nullable Types)
- awesome-go - null - Nullable Go types that can be marshalled/unmarshalled to/from JSON. (Data Structures and Algorithms / Nullable Types)
- awesome-go-extra - null - 07-04T21:18:45Z|2021-11-09T16:04:18Z| (Generators / Nullable Types)
README
# Nullable Go types
[![Go Reference](https://pkg.go.dev/badge/github.com/emvi/null?status.svg)](https://pkg.go.dev/github.com/emvi/null?status)
[![CircleCI](https://circleci.com/gh/emvi/null.svg?style=svg)](https://circleci.com/gh/emvi/null)
[![Go Report Card](https://goreportcard.com/badge/github.com/emvi/null)](https://goreportcard.com/report/github.com/emvi/null)## 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 mainimport (
"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](CONTRIBUTING.md)
## License
MIT