Documentation ¶
Overview ¶
Example ¶
package main import ( "fmt" "github.com/nebiros/optional" ) func main() { ov := optional.New("something") sv, err := ov.Get() if err != nil { panic(err) } fmt.Println("sv: " + sv) }
Output:
Example (Nillable) ¶
package main import ( "fmt" "github.com/nebiros/optional" ) func main() { var ( v *string tmp = "something" ) v = &tmp ov := optional.OfNillable(v) if !ov.IsPresent() { panic("v not present") } sv, err := ov.Get() if err != nil { panic(err) } fmt.Println("sv: " + sv) }
Output:
Index ¶
- Variables
- type Optional
- func (o *Optional[T]) Filter(f func(v T) bool) Optional[T]
- func (o *Optional[T]) Get() (T, error)
- func (o *Optional[T]) IsEmpty() bool
- func (o *Optional[T]) IsPresent() bool
- func (o Optional[T]) MarshalJSON() ([]byte, error)
- func (o *Optional[T]) MustGet() T
- func (o *Optional[T]) Or(f func(v T) Optional[T]) Optional[T]
- func (o *Optional[T]) OrElse(other T) T
- func (o *Optional[T]) OrElseErr() (T, error)
- func (o *Optional[T]) Scan(src any) error
- func (o *Optional[T]) UnmarshalJSON(data []byte) error
- func (o *Optional[T]) Value() (driver.Value, error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var (
ErrNoValuePresent = errors.New("optional: no value present")
)
Functions ¶
This section is empty.
Types ¶
type Optional ¶
type Optional[T any] struct { // contains filtered or unexported fields }
func OfNillable ¶
func (Optional[T]) MarshalJSON ¶
func (*Optional[T]) UnmarshalJSON ¶
Click to show internal directories.
Click to hide internal directories.