https://github.com/moveaxlab/go-optional
Our Optional generic implementation for Go. Sometimes we miss Java.
https://github.com/moveaxlab/go-optional
go optional repository
Last synced: 4 months ago
JSON representation
Our Optional generic implementation for Go. Sometimes we miss Java.
- Host: GitHub
- URL: https://github.com/moveaxlab/go-optional
- Owner: moveaxlab
- License: mit
- Created: 2024-02-16T11:30:27.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-02-19T09:15:21.000Z (almost 2 years ago)
- Last Synced: 2025-09-01T20:45:14.761Z (5 months ago)
- Topics: go, optional, repository
- Language: Go
- Homepage:
- Size: 6.84 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Optionals in Go
This is a no nonsense zero dependency generic `Optional` implementation for Go.
## Installation
```bash
go get github.com/moveaxlab/go-optional
```
## Usage
Return optionals from your repositories:
```go
package repository
import (
"context"
"github.com/moveaxlab/go-optional"
)
type Repository interface {
FindById(ctx context.Context, id string) optional.Optional[Entity]
}
type repository struct {}
func (r *repository) FindById(ctx context.Context, id string) optional.Optional[Entity] {
var entity Entity
var entityWasFound bool
// retrieve the entity in some way
if !entityWasFound {
return optional.Empty[Entity]()
}
return optional.Of(&entity)
}
```
The use the optionals in your application logic:
```go
package service
func (s *service) DoSomeStuff(ctx context.Context) {
maybeEntity := s.repository.FindById(ctx, "1")
if maybeEntity.IsPresent() {
// we have the entity!
entity := maybeEntity.Get()
}
}
```
The `Optional` type offers these methods:
- `IsPresent` returns true if there is a value in the optional
- `Get` returns the value contained in the optional, and panics if it's empty
- `OrElseGet` accepts a function in input, and returns the value contained
in the optional if present, or the result of the function otherwise
- `OrElsePanic` returns the value contained in the optional if present,
or panics with a custom error passed in input
- `IfPresent` runs the function passed as argument if the value is present,
passing it the value contained in the optional
- `IfPresentOrElse` behaves like `IfPresent` for the first argument,
but calls the function passed as second argument if the optional is empty