Documentation ¶
Overview ¶
option is a type that represents optional values.
Example (DefaultParameter) ¶
package main import ( "fmt" "github.com/snowmerak/generics-for-go/v2/types/option" ) func main() { add := func(a, b *option.Option[int]) int { return a.UnwrapOr(0) + b.UnwrapOr(0) } r := add(option.Some(4), option.None[int]()) fmt.Println(r) }
Output: 4
Example (IAmEven) ¶
package main import ( "fmt" "github.com/snowmerak/generics-for-go/v2/types/option" ) func main() { a := option.Some(4).AndThen(func(i int) (int, bool) { if i%2 == 0 { return i, true } return 0, false }) fmt.Println(a) }
Output: 4
Example (IAmOdd) ¶
package main import ( "fmt" "github.com/snowmerak/generics-for-go/v2/types/option" ) func main() { a := option.Some(5).AndThen(func(i int) (int, bool) { if i%2 == 0 { return 0, false } return i, true }) fmt.Println(a) }
Output: 0
Index ¶
Examples ¶
- Package (DefaultParameter)
- Package (IAmEven)
- Package (IAmOdd)
- None (Int)
- None (String)
- Option.AndThen (False)
- Option.AndThen (True)
- Option.IsSome (None)
- Option.IsSome (Some)
- Option.Map (NoneFalse)
- Option.Map (NoneTrue)
- Option.Map (SomeFalse)
- Option.Map (SomeTrue)
- Option.MapOr (False)
- Option.MapOr (True)
- Option.Unwrap (False)
- Option.Unwrap (True)
- Option.UnwrapOr (False)
- Option.UnwrapOr (True)
- Some (Int)
- Some (String)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Option ¶
type Option[T any] struct { // contains filtered or unexported fields }
Option is a container for a value of type T.
func None ¶
None returns a new Option with no value.
Example (Int) ¶
package main import ( "fmt" "github.com/snowmerak/generics-for-go/v2/types/option" ) func main() { a := option.None[int]() b := a.Unwrap() fmt.Println(b) }
Output: 0
Example (String) ¶
package main import ( "fmt" "github.com/snowmerak/generics-for-go/v2/types/option" ) func main() { a := option.None[string]() b := a.Unwrap() fmt.Println(b) }
Output: ""
func Some ¶
Some returns a new Option with the given value.
Example (Int) ¶
package main import ( "fmt" "github.com/snowmerak/generics-for-go/v2/types/option" ) func main() { a := option.Some(1) b := a.Unwrap() fmt.Println(b) }
Output: 1
Example (String) ¶
package main import ( "fmt" "github.com/snowmerak/generics-for-go/v2/types/option" ) func main() { a := option.Some("example string") b := a.Unwrap() fmt.Println(b) }
Output: example string
func (*Option[T]) AndThen ¶
MapOrElse returns new Option by applying the given function to old Option.
Example (False) ¶
package main import ( "fmt" "github.com/snowmerak/generics-for-go/v2/types/option" ) func main() { a := option.Some(1) b := a.AndThen(func(i int) (int, bool) { return i + 1, false }) c := b.Unwrap() fmt.Println(c) }
Output: 0
Example (True) ¶
package main import ( "fmt" "github.com/snowmerak/generics-for-go/v2/types/option" ) func main() { a := option.Some(1) b := a.AndThen(func(i int) (int, bool) { return i + 1, true }) c := b.Unwrap() fmt.Println(c) }
Output: 2
func (*Option[T]) IsSome ¶
IsSome returns true if the Option has a value.
Example (None) ¶
package main import ( "fmt" "github.com/snowmerak/generics-for-go/v2/types/option" ) func main() { a := option.None[int]() b := a.IsSome() fmt.Println(b) }
Output: false
Example (Some) ¶
package main import ( "fmt" "github.com/snowmerak/generics-for-go/v2/types/option" ) func main() { a := option.Some(1) b := a.IsSome() fmt.Println(b) }
Output: true
func (*Option[T]) Map ¶
Map transforms the Option by applying the given function to the value. if Option does not have a value, do not applying. if given function returns false, Option will be changed to None.
Example (NoneFalse) ¶
package main import ( "fmt" "github.com/snowmerak/generics-for-go/v2/types/option" ) func main() { a := option.None[int]() a.Map(func(i int) (int, bool) { return i + 1, false }) c := a.Unwrap() fmt.Println(c) }
Output: 0
Example (NoneTrue) ¶
package main import ( "fmt" "github.com/snowmerak/generics-for-go/v2/types/option" ) func main() { a := option.None[int]() a.Map(func(i int) (int, bool) { return i + 1, true }) c := a.Unwrap() fmt.Println(c) }
Output: 1
Example (SomeFalse) ¶
package main import ( "fmt" "github.com/snowmerak/generics-for-go/v2/types/option" ) func main() { a := option.Some(1) a.Map(func(i int) (int, bool) { return i + 1, false }) c := a.Unwrap() fmt.Println(c) }
Output: 0
Example (SomeTrue) ¶
package main import ( "fmt" "github.com/snowmerak/generics-for-go/v2/types/option" ) func main() { a := option.Some(1) a.Map(func(i int) (int, bool) { return i + 1, true }) c := a.Unwrap() fmt.Println(c) }
Output: 2
func (*Option[T]) MapOr ¶
MapOr transforms the Option by applying the given function to the value. if Option does not have a value or given function returns false, Option will be have defaultValue.
Example (False) ¶
package main import ( "fmt" "github.com/snowmerak/generics-for-go/v2/types/option" ) func main() { a := option.Some(1) a.MapOr(func(i int) (int, bool) { return i + 1, false }, -99) fmt.Println(a) }
Output: -99
Example (True) ¶
package main import ( "fmt" "github.com/snowmerak/generics-for-go/v2/types/option" ) func main() { a := option.Some(1) a.MapOr(func(i int) (int, bool) { return i + 1, true }, -99) fmt.Println(a) }
Output: 2
func (*Option[T]) Unwrap ¶
func (o *Option[T]) Unwrap() (rs T)
Unwrap returns the value of the Option. if Option does not have a value, returns zero value of T.
Example (False) ¶
package main import ( "fmt" "github.com/snowmerak/generics-for-go/v2/types/option" ) func main() { a := option.None[int]() b := a.Unwrap() fmt.Println(b) }
Output: 0
Example (True) ¶
package main import ( "fmt" "github.com/snowmerak/generics-for-go/v2/types/option" ) func main() { a := option.Some(1) b := a.Unwrap() fmt.Println(b) }
Output: 1
func (*Option[T]) UnwrapOr ¶
func (o *Option[T]) UnwrapOr(defaultValue T) T
UnwrapOr returns the value of the Option or the given value.
Example (False) ¶
package main import ( "fmt" "github.com/snowmerak/generics-for-go/v2/types/option" ) func main() { a := option.None[int]() b := a.UnwrapOr(99) fmt.Println(b) }
Output: 99
Example (True) ¶
package main import ( "fmt" "github.com/snowmerak/generics-for-go/v2/types/option" ) func main() { a := option.Some(1) b := a.UnwrapOr(0) fmt.Println(b) }
Output: 1