Skip to content

Result types for Go; because (T, error) can be considered a ✨ monad ✨.

License

Notifications You must be signed in to change notification settings

kdungs/go-result

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Result types for Go

Because (T, error) can be considered a ✨ monad ✨.

Your scientists were so preoccupied with whether or not they could that they didn't stop to think if they should!

Imagine Go code without if err != nil. Would this be sacrilege? You might think so. For me, it was a fun exercise to get to know Go generics. Don't try this at home in prod.

The central idea is to treat (T, error) as a monad. There are two separate packages that implement this idea in different ways:

  1. package result exposes a dedicated type R[T] that wraps (T, error). On top of that, it implements functions to perform computations on R[T]. Since Go does not have generic member functions, those are free functions.
  2. package then offers functionality to (lazily) compose functions that return (T, error).

There's actually an ongoing discussion around how to improve error handling in Go 2.

About

Result types for Go; because (T, error) can be considered a ✨ monad ✨.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages