Package opt implements an optional-type, for the Go programming language. In other programming languages, an optional-type might be know as: a option-type, or a maybe-type.
Go to file
Charles Iliya Krempeaux bfb598fbd1 opt.Map() — I would prefer that map be a method, but the current implementation of Go generatics does not seem to support that 2022-08-10 09:18:03 -07:00
LICENSE change contact info in LICENSE 2022-08-08 08:57:37 -07:00
README.md opt.Optional[T] 2022-08-08 10:21:23 -07:00
go.mod opt.Optional[T] 2022-08-08 10:01:41 -07:00
go.sum opt.Optional[T] 2022-08-08 10:01:41 -07:00
map.go opt.Map() — I would prefer that map be a method, but the current implementation of Go generatics does not seem to support that 2022-08-10 09:18:03 -07:00
map_test.go opt.Map() — I would prefer that map be a method, but the current implementation of Go generatics does not seem to support that 2022-08-10 09:18:03 -07:00
optional.go opt.Optional[T] 2022-08-08 10:01:41 -07:00
optional_get_test.go opt.Optional[T] 2022-08-08 10:01:41 -07:00
optional_gostring_test.go opt.Optional[T] 2022-08-08 10:01:41 -07:00
optional_whennothing_test.go opt.Optional[T] 2022-08-08 10:01:41 -07:00
optional_whensomething_test.go opt.Optional[T] 2022-08-08 10:01:41 -07:00

README.md

go-opt

Package opt implements an optional-type, for the Go programming language.

In other programming languages, an optional-type might be know as: a option type, or a maybe type.

Documention

Online documentation, which includes examples, can be found at: http://godoc.org/github.com/reiver/go-opt

GoDoc

Examples

Here is an example optional-type that can hold a string:

var op opt.Optional[string] // the default value is nothing.

// ...

if opt.Nothing[string]() == op {
	fmt.Println("nothing")
}

// ...

op = opt.Something("Hello world! 👾")

// ...

switch op {
case op.Nothing[string]():
	//@TODO
case op.Something("apple"):
	//@TODO
case op.Something("banana"):
	//@TODO
case op.Something("cherry"):
	//@TODO
default:
	//@TODO
}

// ...

value, found := op.Get()
if {
	fmt.Println("VALUE:", value)
} else {
	fmt.Println("nothing")
}