lo for Go: Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Library lo for Go: Generic Utilities (Lodash for Go)

\n\n

In 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.

\n\n

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.

\n\n

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.

\n\n

Installation

\n\n

Installing 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).

\n\n
go get github.com/samber/lo@latest
\n\n

After executing this command, the library will be added to your go.mod file. You can now import it into your code:

\n\n
import "github.com/samber/lo"
\n\n

Quick Start

\n\n

Let's write a simple program that uses lo to filter and transform a slice of numbers.

\n\n
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\n

As you can see, the code has become much more declarative and readable. We avoided writing three separate for loops.

\n\n

Main Methods and Functions

\n\n

Below is a list of key functions of the lo library with usage examples. Most functions take a slice and a callback function.

\n\n

1. lo.Filter

\n

Signature: func Filter[T any](collection []T, predicate func(item T, index int) bool) []T

\n

What it does: Creates a new slice containing only those elements of the original slice for which the predicate function returned true.

\n
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                    

Recommendations