result

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 5, 2026 License: Apache-2.0 Imports: 0 Imported by: 0

Documentation

Overview

Package result provides a Result type representing either a successful value or an error.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Result

type Result[T any] struct {
	// contains filtered or unexported fields
}

Result holds either a successful value of type T or an error.

func Err

func Err[T any](err error) Result[T]

Err returns a failed Result containing err. Panics if err is nil.

func FlatMap

func FlatMap[T any, R any](r Result[T], fn func(T) Result[R]) Result[R]

FlatMap applies fn to the value inside r and returns the resulting Result. If r is an error, FlatMap returns an error Result of type R.

func Map

func Map[T any, R any](r Result[T], fn func(T) R) Result[R]

Map applies fn to the value inside r and returns a new Result containing the result. If r is an error, Map returns an error Result of type R.

Example
package main

import (
	"fmt"

	"github.com/mikehelmick/go-functional/result"
)

func main() {
	r := result.Map(result.OK(21), func(v int) int { return v * 2 })
	fmt.Println(r.MustGet())
}
Output:
42

func OK

func OK[T any](v T) Result[T]

OK returns a successful Result containing v.

Example
package main

import (
	"fmt"

	"github.com/mikehelmick/go-functional/result"
)

func main() {
	r := result.OK(42)
	if v, err := r.Get(); err == nil {
		fmt.Println(v)
	}
}
Output:
42

func Of

func Of[T any](v T, err error) Result[T]

Of wraps the idiomatic Go (value, error) pair into a Result.

Example
package main

import (
	"fmt"
	"strconv"

	"github.com/mikehelmick/go-functional/result"
)

func main() {
	r := result.Of(strconv.Atoi("123"))
	fmt.Println(r.MustGet())
}
Output:
123

func (Result[T]) Error

func (r Result[T]) Error() error

Error returns the error, or nil if the Result is successful.

func (Result[T]) Get

func (r Result[T]) Get() (T, error)

Get returns the value and nil error on success, or the zero value of T and the error on failure.

func (Result[T]) IsErr

func (r Result[T]) IsErr() bool

IsErr reports whether the Result holds an error.

func (Result[T]) IsOK

func (r Result[T]) IsOK() bool

IsOK reports whether the Result holds a value.

func (Result[T]) MustGet

func (r Result[T]) MustGet() T

MustGet returns the value. It panics if the Result holds an error.

func (Result[T]) OrElse

func (r Result[T]) OrElse(defaultVal T) T

OrElse returns the value if successful, otherwise returns defaultVal.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL