https://github.com/lukagiorgadze/gonull
Go package simplifies nullable fields handling using Go Generics.
https://github.com/lukagiorgadze/gonull
go golang json null nullable pointers testing
Last synced: about 1 year ago
JSON representation
Go package simplifies nullable fields handling using Go Generics.
- Host: GitHub
- URL: https://github.com/lukagiorgadze/gonull
- Owner: LukaGiorgadze
- License: mit
- Created: 2023-04-30T16:43:09.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2025-02-27T11:04:03.000Z (over 1 year ago)
- Last Synced: 2025-03-29T16:05:30.832Z (about 1 year ago)
- Topics: go, golang, json, null, nullable, pointers, testing
- Language: Go
- Homepage: https://pkg.go.dev/github.com/LukaGiorgadze/gonull/v2
- Size: 63.5 KB
- Stars: 113
- Watchers: 5
- Forks: 10
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Go Nullable with Generics
[](https://pkg.go.dev/github.com/LukaGiorgadze/gonull)    [](https://codecov.io/gh/LukaGiorgadze/gonull)
## Go package simplifies nullable fields handling with Go Generics.
`gonull` is a Go package that provides type-safe handling of nullable values using generics. It's designed to work seamlessly with JSON and SQL operations, making it perfect for web services and database interactions.
## Features
- 🎯 Type-safe nullable values using Go generics
- 💡 Omitzero support
- 🔄 Built-in JSON marshaling/unmarshaling
- 📊 SQL database compatibility
- ✨ Zero dependencies
## Usage
```bash
go get github.com/LukaGiorgadze/gonull/v2
```
### Example
```go
package main
import (
"encoding/json"
"fmt"
"github.com/LukaGiorgadze/gonull"
)
type MyCustomInt int
type MyCustomFloat32 float32
type Person struct {
Name string `json:"name"`
Age gonull.Nullable[MyCustomInt] `json:"age"`
Address gonull.Nullable[string] `json:"address"`
Height gonull.Nullable[MyCustomFloat32] `json:"height"`
IsZero gonull.Nullable[bool] `json:"is_zero,omitzero"` // This property will be omitted from the output since it's not present in jsonData.
}
func main() {
jsonData := []byte(`
{
"name": "Alice",
"age": 15,
"address": null,
"height": null
}`)
var person Person
json.Unmarshal(jsonData, &person)
fmt.Printf("Unmarshalled Person: %+v\n", person)
marshalledData, _ := json.Marshal(person)
fmt.Printf("Marshalled JSON: %s\n", string(marshalledData))
// Output:
// Unmarshalled Person: {Name:Alice Age:15 Address: Height:0 IsZero:false}
// Marshalled JSON: {"name":"Alice","age":15,"address":null,"height":null}
// As you see, IsZero is not present in the output, because we used the omitzero tag introduced in go v1.24.
}
```
### Database example
```go
type User struct {
Name gonull.Nullable[string]
Age gonull.Nullable[int]
}
func main() {
// ...
rows, err := db.Query("SELECT id, name, age FROM users")
if err != nil {
log.Fatal(err)
}
defer rows.Close()
for rows.Next() {
var user User
err := rows.Scan(&user.Name, &user.Age)
if err != nil {
log.Fatal(err)
}
fmt.Printf("ID: %d, Name: %v, Age: %v\n", user.Name.Val, user.Age.Val)
}
// ...
}
```