Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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: 6 days 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 (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-02-15T19:38:28.000Z (9 months ago)
- Last Synced: 2024-10-27T23:27:00.140Z (11 days ago)
- Topics: go, golang, json, null, nullable, pointers, testing
- Language: Go
- Homepage: https://pkg.go.dev/github.com/LukaGiorgadze/gonull
- Size: 64.5 KB
- Stars: 75
- Watchers: 3
- Forks: 9
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Go Nullable with Generics
[![PkgGoDev](https://pkg.go.dev/badge/github.com/LukaGiorgadze/gonull)](https://pkg.go.dev/github.com/LukaGiorgadze/gonull) ![mod-verify](https://github.com/LukaGiorgadze/gonull/workflows/mod-verify/badge.svg) ![golangci-lint](https://github.com/LukaGiorgadze/gonull/workflows/golangci-lint/badge.svg) ![staticcheck](https://github.com/LukaGiorgadze/gonull/workflows/staticcheck/badge.svg) ![gosec](https://github.com/LukaGiorgadze/gonull/workflows/gosec/badge.svg) [![codecov](https://codecov.io/gh/LukaGiorgadze/gonull/branch/main/graph/badge.svg?token=76089e7b-f137-4459-8eae-4b48007bd0d6)](https://codecov.io/gh/LukaGiorgadze/gonull)
## Go package simplifies nullable fields handling with Go Generics.
Package gonull provides a generic `Nullable` type for handling nullable values in a convenient way.
This is useful when working with databases and JSON, where nullable values are common.
Unlike other nullable libraries, gonull leverages Go's generics feature, enabling it to work seamlessly with any data type, making it more versatile and efficient.## Why gonull
- Use of Go's generics allows for a single implementation that works with any data type.
- Seamless integration with `database/sql` and JSON marshalling/unmarshalling.
- Reduces boilerplate code and improves code readability.## Usage
```bash
go get github.com/LukaGiorgadze/gonull
```### Example
```go
package mainimport (
"encoding/json"
"fmt""github.com/LukaGiorgadze/gonull"
)type MyCustomInt int
type MyCustomFloat32 float32type Person struct {
Name string
Age gonull.Nullable[MyCustomInt]
Address gonull.Nullable[string]
Height gonull.Nullable[MyCustomFloat32]
}func main() {
jsonData := []byte(`{"Name":"Alice","Age":15,"Address":null,"Height":null}`)var person Person
err := json.Unmarshal(jsonData, &person)
if err != nil {
panic(err)
}
fmt.Printf("Unmarshalled Person: %+v\n", person)marshalledData, err := json.Marshal(person)
if err != nil {
panic(err)
}
fmt.Printf("Marshalled JSON: %s\n", string(marshalledData))
}```
### 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)
}
// ...
}
```