go-optional is a lightweight Go package that provides a generic Optional[T] type for handling values that may or may not be present. It offers a type-safe way to avoid nil pointer issues, with methods to check for value presence and retrieve values safely. Built with Go generics, it supports any type and follows idiomatic Go design principles.
- Generic
Optional[T]type for any Go type. - Methods to check value presence (
IsSome,IsNone). - Safe value retrieval with
Value(using a default) orMustValue(error handling).
To use go-optional in your Go project, ensure you have Go 1.18 or later (required for generics). Add the package to your project:
go get github.com/GreatValueCreamSoda/go-optionalOptional types can be created using the Some() and None() containing and not containing a value respectively. Below are examples demonstrating common use of them and what functions optional types support.
package main
import (
"fmt"
go_optional "github.com/GreatValueCreamSoda/go-optional"
)
func main() {
// Create an Optional with a value
some := go_optional.Some(42)
if some.IsSome() {
fmt.Println(some.Value(0)) // Output: 42
}
// Create an Optional with no value
none := go_optional.None[string]()
if none.IsNone() {
fmt.Println(none.Value("default")) // Output: default
}
// Use MustValue for error handling
someStr := go_optional.Some("hello")
val, err := someStr.MustValue()
if err != nil {
fmt.Println("Error:", err)
} else {
fmt.Println("Value:", val) // Output: Value: hello
}
// Handle None with MustValue
noneInt := go_optional.None[int]()
_, err = noneInt.MustValue()
fmt.Println(err) // Output: called MustValue() on None
}The package includes comprehensive unit tests covering all methods and edge cases, including zero values and error handling. The following command is used to run all tests.
go test -v