package
module
Version:
v0.0.2
Opens a new window with list of versions in this module.
Published: Jan 27, 2025
License: MIT
Opens a new window with license information.
Imports: 1
Opens a new window with list of imports.
Imported by: 0
Opens a new window with list of known importers.
README
¶
opt

The opt package provides a generic type for optional values.
Features
- Generic:
T[U] struct holds any type of value.
- Sortable: implements "cmp"-like helper functions
Less and Compare (only for U satisfied cmp.Ordered constraint).
- Serializable: TODO
Usage
Here's a simple example demonstrating how to use the opt package:
Example
package main
import (
"fmt"
"github.com/WinPooh32/opt"
)
func main() {
var v opt.T[int]
v = opt.Wrap(1)
if v.Set() {
fmt.Println(v.Value())
}
}
Documentation
¶
package main
import (
"fmt"
"github.com/WinPooh32/opt"
)
func main() {
o1 := opt.Wrap(1)
if v, ok := opt.Unwrap(o1); ok {
fmt.Println(v)
}
o2 := opt.Wrap(2)
if o2.Set() {
fmt.Println(o2.Value())
}
fmt.Println(opt.Less(o1, o2))
fmt.Println(opt.Compare(o1, o2))
}
Output:
1
2
true
-1
Compare returns
-1 if x is less than y,
0 if x equals y,
+1 if x is greater than y
Unset value is always considered less.
Less returns true when x is less than y.
Unset value is always considered less.
Unwrap returns x content as tuple as the value and set indicator.
T contains optional value of any type.
Empty value can be used.
Empty returns empty container of any type U.
func Wrap[U any](value U) T[U]
Wrap wraps the value as optional type.
Set returns true when the value is set.
Value returns contained value.
Source Files
¶
Click to show internal directories.
Click to hide internal directories.