omit is a simple library to handle optional and nullable struct field values for json serialization and deserialization in golang.
go get github.com/disgoorg/omit
package main
import (
"encoding/json"
"fmt"
"github.com/disgoorg/omit"
)
type User struct {
ID int `json:"id"`
Name string `json:"name"`
Email omit.Omit[*string] `json:"email,omitzero"`
}
func main() {
u := User{
ID: 1,
Name: "John Doe",
Email: omit.NewPtr("test@example.com"),
}
// Marshal
data, err := json.Marshal(u)
if err != nil {
panic(err)
}
fmt.Println(string(data))
}
Documentation is wip and can be found under:
For help feel free to open an issue or reach out on Discord
Contributions are welcomed but for bigger changes we recommend first reaching out via Discord or create an issue to discuss your problems, intentions and ideas.