https://github.com/kazhuravlev/optional
Provides a type for working with optional variables, fields in structures, functions and database models.
https://github.com/kazhuravlev/optional
Last synced: 2 days ago
JSON representation
Provides a type for working with optional variables, fields in structures, functions and database models.
- Host: GitHub
- URL: https://github.com/kazhuravlev/optional
- Owner: kazhuravlev
- License: mit
- Created: 2024-01-20T21:44:50.000Z (about 2 years ago)
- Default Branch: master
- Last Pushed: 2024-08-14T16:32:50.000Z (over 1 year ago)
- Last Synced: 2025-04-01T16:16:18.868Z (12 months ago)
- Language: Go
- Size: 15.6 KB
- Stars: 7
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-go-with-stars - optional - 02-15 | (Utilities / Utility/Miscellaneous)
- awesome-go - optional - Optional struct fields and vars. (Utilities / Utility/Miscellaneous)
- fucking-awesome-go - optional - Optional struct fields and vars. (Utilities / Utility/Miscellaneous)
- awesome-go-cn - optional
- awesome-go - optional - Optional struct fields and vars. (Utilities / Utility/Miscellaneous)
README
# Optional values for Go projects
[](https://pkg.go.dev/github.com/kazhuravlev/optional)
[](https://github.com/kazhuravlev/optional/blob/master/LICENSE)
[](https://github.com/kazhuravlev/optional/actions/workflows/tests.yml?query=branch%3Amaster)
[](https://goreportcard.com/report/github.com/kazhuravlev/optional)
[](https://codecov.io/gh/kazhuravlev/optional)
We often need tools that help us work with optional values. For example, for optional fields in requests/responses from
APIs (JSON), optional values in YAML/JSON configs, or nullable columns in some tables. Typically, we use a pointer for
certain data types, but this does not work well for some scenarios.
This package offers a simple way to define optional fields and provides some helpers for marshalling and unmarshalling
data.
## Features
- JSON marshal/unmarshal
- YAML marshal/unmarshal
- SQL driver value/scan
- Useful functions to create and handle optional values
## Installation
```shell
go get -u github.com/kazhuravlev/optional
```
## Usage
```go
package main
import (
"fmt"
"encoding/json"
"github.com/kazhuravlev/optional"
)
type User struct {
AvatarURL optional.Val[string] `json:"avatar_url"`
// some fields...
}
func main() {
req := []byte(`{"avatar_url": "https://example.com/img.webp"}`)
var user User
if err := json.Unmarshal(req, &user); err != nil {
panic(err)
}
// Check the presence of value and use it.
if avatar, ok := user.AvatarURL.Get(); ok {
fmt.Println("User have avatar", avatar)
} else {
fmt.Println("User have no avatar")
}
// Just check that value presented.
if !user.AvatarURL.HasVal() {
fmt.Println("User have no avatar")
}
// Use default value in case of it did not presented.
fmt.Println("Avatar of this user is:", user.AvatarURL.ValDefault("https://example.com/default.webp"))
// Use this api to adapt value to pointer. It will return nil when value not provided.
avatarPtr := user.AvatarURL.AsPointer()
fmt.Printf("Pointer to avatar: %#v\n", avatarPtr)
}
```