https://github.com/hymkor/go-minimal-optional
The minimal `optional` package for golang
https://github.com/hymkor/go-minimal-optional
generics go golang optional
Last synced: 24 days ago
JSON representation
The minimal `optional` package for golang
- Host: GitHub
- URL: https://github.com/hymkor/go-minimal-optional
- Owner: hymkor
- License: mit
- Created: 2023-04-14T03:49:44.000Z (about 2 years ago)
- Default Branch: master
- Last Pushed: 2024-03-10T01:15:08.000Z (about 1 year ago)
- Last Synced: 2025-02-10T15:50:55.712Z (3 months ago)
- Topics: generics, go, golang, optional
- Language: Go
- Homepage: https://pkg.go.dev/github.com/hymkor/go-minimal-optional
- Size: 18.6 KB
- Stars: 1
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://pkg.go.dev/github.com/hymkor/go-minimal-optional)
go-minimal-optional
===================This package has only two constructors (Some, None) and three methods (IfSome, IsNone, Match).
+ `optional.Value` is an array whose size is 0 or 1. Therefore, the contents can be handled with for-range even in versions below Go 1.22.
+ `optional.Option` was renamed to `optional.Value````example.go
package mainimport (
"github.com/hymkor/go-minimal-optional"
)func test(x optional.Value[int]) {
x.IfSome(func(v int) {
println(" IfSome: it has a value:", v)
})for _, v := range x {
println(" for-range(ready for v1.18): it has a value:", v)
}// GOEXPRIMENT=rangefunc is required to build following line.
for v := range x.Each {
println(" for-range(v1.22 X:rangefunc): it has a value:", v)
}if x.IsNone() {
println(" IsNone: it does not have a value")
}x.Match(func(v int) {
println(" Match: it has a value:", v)
}, func() {
println(" Match: it does not hava a value")
})println()
}func main() {
println("None[int]")
test(optional.None[int]())println("Some(4)")
test(optional.Some(4))
}
```**env GOEXPERIMENT=rangefunc go run example.go**
```env GOEXPERIMENT=rangefunc go run example.go|
None[int]
IsNone: it does not have a value
Match: it does not hava a valueSome(4)
IfSome: it has a value: 4
for-range(ready for v1.18): it has a value: 4
for-range(v1.22 X:rangefunc): it has a value: 4
Match: it has a value: 4```