Getting started
samber/lo is a Lodash-style utility library for Go 1.18+ that leverages generics to provide type-safe helper functions. The library is organized into several packages, each serving different use cases.
๐ Installโ
go get -u github.com/samber/lo@v1
๐งข Core Package (lo
)โ
The main package provides immutable utility functions for slices, maps, strings, math operations, and more. It's the core of the library with over 300+ functions.
import "github.com/samber/lo"
// Example: Map a slice of numbers to their squares
numbers := []int{1, 2, 3, 4, 5}
squared := lo.Map(numbers, func(x int, _ int) int {
return x * x
})
// Result: [1, 4, 9, 16, 25]
๐ Iter Package (lo/it
)โ
The it
package provides Go 1.23+ sequence helpers with lazy evaluation, offering over 100 functions for efficient iteration without buffering.
// Future usage (Go 1.23+)
import (
"iter"
loi "github.com/samber/lo/it"
)
seqIn := iter.Range(0, 1000)
// Lazy iteration without buffering
seqOut := loi.Filter(seqIn, func(x int) bool {
return x%2 == 0
})
๐ฃ Mutable Package (lo/mutable
)โ
The mutable package provides in-place operations that modify collections directly, useful for performance-critical scenarios.
import lom "github.com/samber/lo/mutable"
// Filter in-place (modifies the original slice)
numbers := []int{1, 2, 3, 4, 5}
lom.Filter(&numbers, func(x int) bool {
return x%2 == 0
})
// Result: [2, 4]
๐๏ธ Parallel Package (lo/parallel
)โ
The parallel package enables concurrent processing of collections with built-in worker pools, perfect for CPU-intensive operations.
import lop "github.com/samber/lo/parallel"
// Process items concurrently (4 workers by default)
results := lop.Map(numbers, 4, func(x int) int {
// Some expensive operation
return expensiveOperation(x)
})
โ Key Benefitsโ
- Type-safe with generics
- Immutable by default (main package)
- Performance optimized with parallel and mutable variants
- Comprehensive with 500+ utility functions
- Lazy evaluation with
iter
std package (Go >= 1.23) - Minimal dependencies zero dependencies outside the Go standard library
๐ Next Stepsโ
- Check the Go documentation for complete API reference
- Explore examples in the repository
- Choose the right sub-package for your use case