Opt is a Go package for safe abstractions over optional values.
Inspired by the Option type in Rust and follows the same ideas and function signatures.
- Represent explicitly set values. For example:
{"b":2,"a":null}
and{"b":2}
would be different states fora
- explicit and implicitNone
. - All the encoding and decoding functionality: json, gob, sql, text & binary.
- Adapters to construct options from pointers, zero values, and proto messages.
- No reflection.
go get github.com/metafates/opt
See example_test.go for more examples.
package main
import (
"fmt"
"time"
"github.com/metafates/opt"
)
type User struct {
Birth opt.Opt[time.Time]
}
func (u User) Age() opt.Opt[int] {
return opt.Map(u.Birth, func(t time.Time) int {
return time.Now().Year() - t.Year()
})
}
func isAdult(age int) bool {
return age >= 18
}
func getUser(id int) opt.Opt[User] {
// ...
}
func main() {
if opt.AndThen(getUser(42), User.Age).IsSomeAnd(isAdult) {
fmt.Println("🍺!")
}
}