Skip to content

leavez/go-optional

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-optional

The optional type for golang, implemented with generics (go 1.18).

TLDR

var v optional.Type[int] = optional.New(123)
if unwrapped, ok := v.Value(); ok {
    unwrapped // 123
}

v.IsNil()      // false
v.ForceValue() // 123

Methods

initializer

  • optional.New(XX)

If XX is nil, it returns Nil

  • optional.Nil()
  • (optional.FromPtr(XXX) returns optional.Nil() if pointer is nil)

value

  • Value()
var v = optional.New(123)
if unwrapped, ok := v.Value(); ok {
    unwrapped // 123
}
  • ForceValue()

  • IsNil()

  • ValueOrDefault(XX)

  • ValueOrLazyDefault(f)

transform

  • Map
optional.Map( optional.New(123), func(t int) string {
    return fmt.Sprintf("hello %d", t)
}) // optional.Type[string]
  • Compact
var v = optional.New(optional.New(123))
optional.Compact(v) // optional.Type[int]

serialization

json Marshal and Umarshal supported.

If Nil, the encoded json data is null.

License

MIT

About

optional type for golang

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages