gitmyhub

lo

Go ★ 21k updated 13h ago

💥 A Lodash-style Go library based on Go 1.18+ Generics (map, filter, contains, find...)

lo is a Go utility library that adds Lodash-style helpers like Filter, Map, GroupBy, and Uniq for slices, maps, and channels, all type-safe using Go generics, with no external dependencies.

Gosetup: easycomplexity 2/5

lo is a utility library for the Go programming language by Samuel Berthe. It brings to Go the kind of compact, expressive helpers that JavaScript developers know from Lodash. The starting point is Go 1.18, the version that introduced generics. Generics let you write one function that works for any element type, which is what makes a clean Filter, Map, and Reduce style possible in Go without rewriting the same function for every type.

The library is a single Go module with a few sub-packages. The main package adds dozens of small functions for working with slices, maps, strings, channels, tuples, time durations, and math. There is a parallel sub-package called lo/parallel for running those helpers across goroutines, a mutable sub-package called lo/mutable that modifies the input in place when that is faster, and an iterator-based sub-package called lo/it. Installation is one go get command, and the library has no dependencies outside the Go standard library. The author keeps it at v1 and follows semantic versioning, so exported APIs will not change before v2.

The slice section alone covers familiar moves like Filter, Map, Reduce, Uniq, GroupBy, Chunk, Flatten, Reverse, Shuffle, Take, Drop, Reject, Count, Replace, and Compact, along with more specialised ones like Window, Sliding, PartitionBy, and CountValuesBy. The map section adds operations such as Keys, Values, PickBy, OmitBy, Entries, Invert, Assign, MapKeys, and MapValues. There are set helpers such as Contains, Every, Some, Intersect, Difference, Union, and Without. There are string casers for PascalCase, CamelCase, KebabCase, and SnakeCase. There are math helpers for Sum, Product, Mean, Mode, Range, and Clamp. There are channel helpers such as FanIn, FanOut, Buffer, and Generator for concurrent code.

The author notes that a few of the helpers overlap with what is now in Go's standard slices and maps packages, but argues the library still offers many more shapes beyond those. You would reach for lo if you write Go and find yourself writing the same small loops to filter, deduplicate, group, or transform collections, and would prefer one-line helpers that read clearly. The author also points to sibling libraries: samber/mo for option and result types, samber/do for dependency injection, and samber/ro for reactive streams over infinite data sources.

Where it fits