Getting started · Section 01 / 01
What is fp-go?
A comprehensive functional programming library for Go — strongly influenced by fp-ts, built around small, pure, composable functions.
// Approach
Many small, pure functions with no hidden side effects.
// Side effects
Isolated into lazy IO-style computations.
// Composition
A consistent set of combinators across every data type.
01
Difficulty · BeginnerQuick example.
Handle errors functionally with Either. Map transforms the success branch; errors flow through untouched.
example.gotested
import (
"errors"
"github.com/IBM/fp-go/either"
"github.com/IBM/fp-go/function"
)
// Pure function that can fail
func divide(a, b int) either.Either[error, int] {
if b == 0 {
return either.Left[int](errors.New("division by zero"))
}
return either.Right[error](a / b)
}
// Compose operations safely
result := function.Pipe2(
divide(10, 2),
either.Map(func(x int) int { return x * 2 }),
either.GetOrElse(func() int { return 0 }),
)
// result = 10What this demonstrates.
- Express operations that can fail using the
Eithertype - Chain operations together safely
- Handle errors explicitly without nested
ifstatements - Write pure, composable functions