go-fp provides generic, chainable wrappers in Go for semi-functional programming with robust error handling, enabling a clear, composable style of coding.
go-fp offers two packages:
immutable: For immutable value types and chaining pure functions.
mutable: For pointer-based types, allowing mutation and error propagation.
The Wrapper[T] type wraps values or pointers with embedded error handling and supports chaining with methods such as Then, FlatMap, and Match.
Requires Go 1.24+ with Go modules.
Use Go modules (Go 1.24+ required):
go get github.com/KeibiSoft/go-fp/immutable
go get github.com/KeibiSoft/go-fp/mutable
For an immutable client and mutable server implementation check examples folder.
package main
import (
"fmt"
"github.com/KeibiSoft/go-fp/immutable/chain"
)
type MyStruct struct {
Value int
}
func (m MyStruct) Inc() (MyStruct, error) {
m.Value++
return m, nil
}
func (m MyStruct) Double() (MyStruct, error) {
m.Value *= 2
return m, nil
}
func main() {
initial := MyStruct{Value: 1}
result, err := chain.Wrap(initial).
Then(MyStruct.Inc).
Then(MyStruct.Double).
Then(MyStruct.Inc).
Result()
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Println("Result:", result.Value) // Output: Result: 5
}
package main
import (
"errors"
"fmt"
"github.com/KeibiSoft/go-fp/mutable/chain"
)
type MyStruct struct {
Value int
}
func (m *MyStruct) Inc() (*MyStruct, error) {
m.Value++
return m, nil
}
func (m *MyStruct) Double() (*MyStruct, error) {
m.Value *= 2
return m, nil
}
func (m *MyStruct) FailIfFive() (*MyStruct, error) {
if m.Value == 5 {
return nil, errors.New("value cannot be 5")
}
return m, nil
}
func main() {
initial := &MyStruct{Value: 1}
errHandler := func(err error) error {
fmt.Println("Handled error:", err)
// Return nil to continue chain despite error, or return err to stop
return err
}
wrapper := chain.New(initial, errHandler)
result, err := wrapper.
Then((*MyStruct).Inc).
Then((*MyStruct).Double).
Then((*MyStruct).Inc).
Then((*MyStruct).FailIfFive).
Result()
if err != nil {
fmt.Println("Chain stopped with error:", err)
return
}
fmt.Println("Result:", result.Value)
}
MIT License
