https://github.com/cospectrum/option
Option type for golang
https://github.com/cospectrum/option
Last synced: 9 months ago
JSON representation
Option type for golang
- Host: GitHub
- URL: https://github.com/cospectrum/option
- Owner: cospectrum
- License: apache-2.0
- Created: 2024-08-05T14:25:37.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-08-05T20:34:01.000Z (almost 2 years ago)
- Last Synced: 2025-07-21T14:02:33.018Z (12 months ago)
- Language: Go
- Size: 19.5 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# option
[![github]](https://github.com/cospectrum/option)
[![goref]](https://pkg.go.dev/github.com/cospectrum/option)
[github]: https://img.shields.io/badge/github-cospectrum/option-8da0cb?logo=github
[goref]: https://pkg.go.dev/badge/github.com/cospectrum/option
Option type for golang
## Install
```sh
go get -u github.com/cospectrum/option
```
Requires Go version `1.22.0` or greater.
## Usage
```go
import (
"fmt"
"github.com/cospectrum/option"
)
func main()
divide := func(numerator, denominator float64) option.Option[float64] {
if denominator == 0.0 {
return option.None[float64]()
}
return option.Some(numerator / denominator)
}
// The return value of the function is an option
result := divide(2.0, 3.0)
// Pattern match to retrieve the value
result.Match(
func(val float64) {
fmt.Printf("Result: %v\n", val)
},
func() {
fmt.Println("Cannot divide by 0")
},
)
}
```
### JSON
```go
type U struct {
Num option.Option[int] `json:"num"`
}
var u U
json.Unmarshal([]byte(`{"num": null}`), &u) // => U{Num: option.None()}
json.Unmarshal([]byte(`{}`), &u) // => U{Num: option.None()}
json.Unmarshal([]byte(`{"num": 0}`), &u) // => U{Num: option.Some(0)}
json.Unmarshal([]byte(`{"num": 3}`), &u) // => U{Num: option.Some(3)}
```