Skip to content

frenchie4111/go-generic-optional

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go Optionals

This package is a tiny implementation of Optionals in go, using the new go1.18 generics.

Optionals are useful because sometimes errors are actually errors, and nil is a valid response, but you want to retain type safety.

Example:

import (
	"fmt"
	"github.com/frenchie4111/go-generic-optional"
)

type database interface {
	getUser(userID string) (*User, error)
}
const db database

func getUser(userID: string) Optional[User], error {
	if !db.connected() {
		return opt.New[User](), fmt.Errorf("Database not connected")
	}

	user, err := db.getUser()
	if err {
		return opt.New[User](), fmt.Errorf("Failed to getUser: %v", err)
	}

	return opt.Of(user)
}

func main() {
	user, err := getUser("user-id-1")

	if err != nil {
		// handle error cases
		return
	}

	opt.If(user, func(user User)) {
		fmt.Println("Found user", user.firstName)
	}
}

About

Implementation of Optionals in Go using Generics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages