$npx -y skills add samber/cc-skills-golang --skill golang-samber-loFunctional programming helpers for Golang using samber/lo — 500+ type-safe generic functions for slices, maps, channels, strings, math, tuples, and concurrency (Map, Filter, Reduce, GroupBy, Chunk, Flatten, Find, Uniq, etc.). Core immutable package (lo), concurrent variants (lo/p
| 1 | **Persona:** You are a Go engineer who prefers declarative collection transforms over manual loops. You reach for `lo` to eliminate boilerplate, but you know when the stdlib is enough and when to upgrade to `lop`, `lom`, or `loi`. |
| 2 | |
| 3 | # samber/lo — Functional Utilities for Go |
| 4 | |
| 5 | Lodash-inspired, generics-first utility library with 500+ type-safe helpers for slices, maps, strings, math, channels, tuples, and concurrency. Zero external dependencies. Immutable by default. |
| 6 | |
| 7 | **Official Resources:** |
| 8 | |
| 9 | - [github.com/samber/lo](https://github.com/samber/lo) |
| 10 | - [lo.samber.dev](https://lo.samber.dev) |
| 11 | - [pkg.go.dev/github.com/samber/lo](https://pkg.go.dev/github.com/samber/lo) |
| 12 | |
| 13 | This skill is not exhaustive. Please refer to library documentation and code examples for more information. For Go package docs, symbols, versions, importers, and known vulnerabilities, → See `samber/cc-skills-golang@golang-pkg-go-dev` skill (`godig`) — prefer it over Context7 for Go package facts. To navigate this library's usage in your own code (definitions, call sites, diagnostics), → See `samber/cc-skills-golang@golang-gopls` skill (`gopls`). Context7 remains a fallback for docs not indexed on pkg.go.dev. |
| 14 | |
| 15 | ## Why samber/lo |
| 16 | |
| 17 | Go's stdlib `slices` and `maps` packages cover ~10 basic helpers (sort, contains, keys). Everything else — Map, Filter, Reduce, GroupBy, Chunk, Flatten, Zip — requires manual for-loops. `lo` fills this gap: |
| 18 | |
| 19 | - **Type-safe generics** — no `interface{}` casts, no reflection, compile-time checking, no interface boxing overhead |
| 20 | - **Immutable by default** — returns new collections, safe for concurrent reads, easier to reason about |
| 21 | - **Composable** — functions take and return slices/maps, so they chain without wrapper types |
| 22 | - **Zero dependencies** — only Go stdlib, no transitive dependency risk |
| 23 | - **Progressive complexity** — start with `lo`, upgrade to `lop`/`lom`/`loi` only when profiling demands it |
| 24 | - **Error variants** — most functions have `Err` suffixes (`MapErr`, `FilterErr`, `ReduceErr`) that stop on first error |
| 25 | |
| 26 | ## Installation |
| 27 | |
| 28 | ```bash |
| 29 | go get github.com/samber/lo |
| 30 | ``` |
| 31 | |
| 32 | | Package | Import | Alias | Go version | |
| 33 | | --- | --- | --- | --- | |
| 34 | | Core (immutable) | `github.com/samber/lo` | `lo` | 1.18+ | |
| 35 | | Parallel | `github.com/samber/lo/parallel` | `lop` | 1.18+ | |
| 36 | | Mutable | `github.com/samber/lo/mutable` | `lom` | 1.18+ | |
| 37 | | Iterator | `github.com/samber/lo/it` | `loi` | 1.23+ | |
| 38 | | SIMD (experimental) | `github.com/samber/lo/exp/simd` | — | 1.25+ (amd64 only) | |
| 39 | |
| 40 | ## Choose the Right Package |
| 41 | |
| 42 | Start with `lo`. Move to other packages only when profiling shows a bottleneck or when lazy evaluation is explicitly needed. |
| 43 | |
| 44 | | Package | Use when | Trade-off | |
| 45 | | --- | --- | --- | |
| 46 | | `lo` | Default for all transforms | Allocates new collections (safe, predictable) | |
| 47 | | `lop` | CPU-bound work on large datasets (1000+ items) | Goroutine overhead; not for I/O or small slices | |
| 48 | | `lom` | Hot path confirmed by `pprof -alloc_objects` | Mutates input — caller must understand side effects | |
| 49 | | `loi` | Large datasets with chained transforms (Go 1.23+) | Lazy evaluation saves memory but adds iterator complexity | |
| 50 | | `simd` | Numeric bulk ops after benchmarking (experimental) | Unstable API, may break between versions | |
| 51 | |
| 52 | **Key rules:** |
| 53 | |
| 54 | - `lop` is for CPU parallelism, not I/O concurrency — for I/O fan-out, use `errgroup` instead |
| 55 | - `lom` breaks immutability — only use when allocation pressure is measured, never assumed |
| 56 | - `loi` eliminates intermediate allocations in chains like `Map → Filter → Take` by evaluating lazily |
| 57 | - For reactive/streaming pipelines over infinite event streams, → see `samber/cc-skills-golang@golang-samber-ro` skill + `samber/ro` package |
| 58 | |
| 59 | For detailed package comparison and decision flowchart, |