https://github.com/robtimus/go-optional
Provides a simple implementation of optionals
https://github.com/robtimus/go-optional
golang optional optionals
Last synced: 3 months ago
JSON representation
Provides a simple implementation of optionals
- Host: GitHub
- URL: https://github.com/robtimus/go-optional
- Owner: robtimus
- License: apache-2.0
- Created: 2025-03-01T14:18:10.000Z (3 months ago)
- Default Branch: master
- Last Pushed: 2025-03-01T14:29:09.000Z (3 months ago)
- Last Synced: 2025-03-01T15:28:18.816Z (3 months ago)
- Topics: golang, optional, optionals
- Language: Go
- Homepage:
- Size: 10.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# go-optional
[](https://github.com/robtimus/go-optional/actions/workflows/build.yml)
[](https://sonarcloud.io/summary/overall?id=robtimus%3Ago-optional)
[](https://sonarcloud.io/summary/overall?id=robtimus%3Ago-optional)
[](https://snyk.io/test/github/robtimus/go-optional)A simple implementation of optionals in Go, based on Java's [Optional](https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/Optional.html) class.
Main differences:
* In Go, only pointers can be `nil`. The function provided to the `Map` operation must return a value of the same type, and therefore will only result in an empty `Optional` if the receiver was already empty. The `MapNillable` operation is added that takes a function that returns a pointer to the `Optional`'s generic type.
* Go does not support method generic types. That means that the `Optional` returned by `Map`, `MapNillable` and `FlatMap` operations cannot have a different generic type. To overcome this functions with the same name are provided that take the `Optional` as first argument. That does mean that method chaining is not always possible and needs to be replaced with call chaining:
```go
// o1 is some Optional
// f1 and f3 has different input and out types
// f2 has the same input and output types
o2 := optional.Map(optional.Map(o1, f1).Map(f2), f3)
```
* Go does not support method overloading. Java's `orElseThrow` is implemented in three ways:
* `OrElsePanic` panics if called on an empty `Optional`.
* `OrElseError` returns a default error if called on an empty `Optional`.
* `OrElseSupplyError` returns an error provided by a function if called on an empty `Optional`.
* Go does not have the concept of streams the way that Java does. Java's `stream` operation has therefore been replaced by `Slice` that returns a slice with 0 or 1 elements, depending on the `Optional`.