Skip to content

Our Optional generic implementation for Go. Sometimes we miss Java.

License

Notifications You must be signed in to change notification settings

moveaxlab/go-optional

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Optionals in Go

This is a no nonsense zero dependency generic Optional<T> implementation for Go.

Installation

go get github.com/moveaxlab/go-optional

Usage

Return optionals from your repositories:

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:

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

About

Our Optional generic implementation for Go. Sometimes we miss Java.

Topics

Resources

License

Stars

Watchers

Forks

Languages