The Go Option package provides an implementation of the Option
type similar to Rust's Option
.
It allows you to represent optional values and handle them in a safe and expressive way, avoiding the need for explicit null checks.
And This is written as go-way
.
Import the option
package in your Go code to use it:
import "github.com/oneofthezombies/option"
You can create Option instances using the Some and None functions. Some is used to wrap a value, while None represents the absence of a value:
value := 42
someOption := option.Some(value)
noneOption := option.None()
You can use the Value method to check if a value is present in an Option. It returns a boolean indicating whether a value is present or not:
// option_test.go
func TestSome(t *testing.T) {
o := Some(1)
v, ok := o.Value()
if !ok {
t.Error("expected option to have value")
}
if v != 1 {
t.Error("expected option value to be 1")
}
}
Here are some basic examples of using the option package:
func TestAll(t *testing.T) {
o := Some("foo")
v, ok := o.Value()
if !ok {
t.Error("expected option to have value")
}
if v != "foo" {
t.Error("expected option value to be foo")
}
o = None[string]()
v, ok = o.Value()
if ok {
t.Error("expected option to not have value")
}
// If None is asigned then the value is the "zero value" of the type. In this case, the zero value of string is an empty string.
// But you don't have to worry about value because we deal with cases where ok is false.
// If value type is a struct, then the zero value is an empty struct.
// And if value type is a pointer, then the zero value is nil.
if v != "" {
t.Error("expected option value to be empty string")
}
}
If you would like to contribute to this project, please follow these steps:
- Fork the repository.
- Create a new branch for your feature or bug fix.
- Make your changes and ensure they pass the existing tests.
- Add new tests if necessary.
- Commit your changes with clear commit messages.
- Push your branch to your fork.
- Create a pull request to the original repository.
This package is licensed under the MIT License. See the LICENSE file for details.