Skip to content

Instantly share code, notes, and snippets.

@nwillc
Created July 15, 2022 14:21
Show Gist options
  • Save nwillc/fae895976665969acf9332e06557239c to your computer and use it in GitHub Desktop.
Save nwillc/fae895976665969acf9332e06557239c to your computer and use it in GitHub Desktop.
Go Error-Result pattern example
type Result[T any] struct {
//...
}
// NewResult for a value.
func NewResult[T any](t T) *Result[T] {...}
// NewResultError creates a Result from a value, error tuple.
func NewResultError[T any](t T, err error) *Result[T] {...}
// NewError for an error.
func NewError[T any](err error) *Result[T] {...}
func (r *Result[T]) Ok() bool {...}
func (r *Result[T]) Error() error {...}
func (r *Result[T]) OrEmpty() T {...}
func Map[T, R any](t *Result[T], transform func(t T) *Result[R]) * Result[R] {...}
func MapError[T, R any](t *Result[T]) *genfuncs.Result[R] {...}
// See genfuncs for full implementation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment