go.eth-p.dev/goptional
Generic Optional (or Go Optional, if you prefer)
goptional
is a package that provides an implementation of an Optional[T]
monad using Go 1.18 generics.
Installation
import (
"go.eth-p.dev/goptional"
)
Why goptional?
- Prevents you from using uninitialized values.
- No code generation required.
- Type-safe API.
- Supports marshalling and unmarshalling. (TODO)
Example
import (
"os"
optional "go.eth-p.dev/goptional"
)
func getConfigDirectory() optional.Optional[string] {
return optional.From(os.LookupEnv("XDG_CONFIG_HOME"))
}
func main() {
configDir := getConfigDirectory().
UnwrapOr("~/.config")
println(configDir)
}
For more detailed examples, feel free to check out the examples directory.