Skip to content

A small Go library containing the functionality given to Java by Optional<>

License

Notifications You must be signed in to change notification settings

dtennander/optional

Repository files navigation

Optional

Build Status Go Report Card GoDoc

This is a small library containing the functionality given to Java by Optional<>. It allows Go users to reduce nil checks and create more readable code.

Usage

package example

import opt "github.com/DiTo04/optional"

type Queue interface{
	peekNext() (person opt.Optional)
}

type Person interface{
	GetName() string
}

// Example of Mapping
func getNameOfNext(queue Queue) (str opt.Optional) {
	person := queue.peekNext()
	return person.Map(func(p Person) string {
		return p.GetName()
	})
}

// Example of default values
func getNameOfNextOrGopher(queue Queue) string {
	return getNameOfNext(queue).OrElse("Gopher!").(string)
}

// Example of Filtering
func isFirstPersonGopher(queue Queue) bool {
	return getNameOfNext(queue).Filter(func(s string) bool {
		return s == "Gopher"
	}).OrElse(false).(bool)
}

Inspiration

About

A small Go library containing the functionality given to Java by Optional<>

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages