Library lo for Go: Generic Utilities (Lodash for Go)
\n\nIn the world of Go development, there is often a need for convenient functions for working with collections that exist in other languages (for example, map, filter, reduce from JavaScript or Python). The Go standard library provides a minimal set of tools, and writing repetitive for loops for each operation becomes tedious. This is where the lo library comes to the rescue — a powerful set of generic utilities inspired by the popular JavaScript library Lodash.
The lo library uses generics (parameterized types), introduced in Go 1.18+, to provide type-safe functions for working with slices, maps, channels, and other data structures. Instead of writing for i := range slice { ... } for each transformation, you can use one line of code: lo.Map(slice, transformFunc). This makes the code more readable, concise, and less error-prone.
Why use lo? Firstly, it saves time: you stop writing boilerplate code. Secondly, it increases reliability: the library's functions are thoroughly tested and cover many edge cases. Thirdly, it ensures consistency: the entire team uses the same functions with clear names, which simplifies code reviews. lo is a must-have for any Go project that actively uses data collections.
Installation
\n\nInstalling the lo library is done in the standard Go way via go get. Make sure you have Go version 1.18 or higher installed (since the library uses generics).
go get github.com/samber/lo@latest\n\nAfter executing this command, the library will be added to your go.mod file. You can now import it into your code:
import "github.com/samber/lo"\n\nQuick Start
\n\nLet's write a simple program that uses lo to filter and transform a slice of numbers.
package main\n\nimport (\n "fmt"\n "github.com/samber/lo"\n)\n\nfunc main() {\n // Original slice of numbers\n numbers := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}\n\n // Filter only even numbers\n evenNumbers := lo.Filter(numbers, func(x int, _ int) bool {\n return x%2 == 0\n })\n fmt.Println("Even numbers:", evenNumbers) // [2 4 6 8 10]\n\n // Double each number\n doubled := lo.Map(evenNumbers, func(x int, _ int) int {\n return x * 2\n })\n fmt.Println("Doubled even numbers:", doubled) // [4 8 12 16 20]\n\n // Sum all numbers\n sum := lo.Sum(doubled)\n fmt.Println("Sum:", sum) // 60\n}\n\nAs you can see, the code has become much more declarative and readable. We avoided writing three separate for loops.
Main Methods and Functions
\n\nBelow is a list of key functions of the lo library with usage examples. Most functions take a slice and a callback function.
1. lo.Filter
\nSignature: func Filter[T any](collection []T, predicate func(item T, index int) bool) []T
What it does: Creates a new slice containing only those elements of the original slice for which the predicate function returned true.
package main\n\nimport (\n "fmt"\n "github.com/samber/lo"\n)\n\nfunc main() {\n names := []string{"Alice", "Bob", "Charlie", "David", "Eve"}\n // Keep only names longer than 3 characters\n longNames := lo.Filter(names, func(name string, _ int) bool {\n return len(name) > 3\n })\n fmt.Println(longNames) // [Alice Cha Also in library
Elasticsearch client for C# (.NET): NEST and Elastic.Clients.Elasticsearch
Monitoring PHP Applications with php-ast and xhprof Libraries
Wolverine for C# (.NET): Complete Guide with Examples
PDF Generation in Java: Overview of the Best Libraries (iText, Apache PDFBox, OpenPDF)