Introducing the Go Generic Utility Library samber/lo
This article introduces the open‑source Go generic utility library samber/lo, explains how to install Go 1.18 beta and the library, and demonstrates common operations such as type conversion, concurrent processing, containment checks, filtering, deduplication, grouping, chunking, and ternary-like expressions with concise code examples.
Go 1.18 is about to be released, bringing the breakthrough feature of generics. With generics, many new possibilities become available.
Introduction
The article introduces the open‑source generic utility library github.com/samber/lo , which has a Lodash‑style API for Go and currently has about 3k stars.
Lodash is a consistent, modular, high‑performance JavaScript utility library; the Go version offers similar functions.
Usage
Install Go 1.18 beta:
$ go install golang.org/dl/go1.18beta2@latest
$ go1.18beta2 downloadInstall the third‑party library:
$ go1.18beta2 github.com/samber/loAfter installation, the library can be used directly.
Type Conversion
Convert a map or slice of one type to another:
import "github.com/samber/lo"
lo.Map[int64, string]([]int64{1, 2, 3, 4}, func(x int64, _ int) string {
return strconv.FormatInt(x, 10)
})
// Output: []string{"1", "2", "3", "4"}Concurrent Processing
Process data concurrently with goroutines while preserving order:
import (
"fmt"
lop "github.com/samber/lo/parallel"
"strconv"
)
func main() {
s := lop.Map[int64, string]([]int64{1, 2, 3, 4}, func(x int64, _ int) string {
return strconv.FormatInt(x, 10)
})
fmt.Println(s)
}
// Output: [1 2 3 4]Containment and Filtering
Check if an element exists in a collection:
present := lo.Contains[int]([]int{0,1,2,3,4,5}, 5)
// Output: trueFilter elements that satisfy a condition:
even := lo.Filter[int]([]int{1,2,3,4}, func(x int, _ int) bool {
return x%2 == 0
})
// Output: []int{2,4}Unique Values
Remove duplicate elements from a slice while preserving order:
uniqValues := lo.Uniq[int]([]int{1,2,2,1})
// Output: []int{1,2}Grouping and Chunking
Group a slice by a custom key function:
groups := lo.GroupBy[int, int]([]int{0,1,2,3,4,5}, func(i int) int {
return i % 3
})
// Output: map[int][]int{0: {0,3}, 1: {1,4}, 2: {2,5}}Chunk a slice into smaller slices:
lo.Chunk[int]([]int{0,1,2,3,4,5}, 2)
// Output: [][]int{{0,1},{2,3},{4,5}}
lo.Chunk[int]([]int{0,1,2,3,4,5,6}, 2)
// Output: [][]int{{0,1},{2,3},{4,5},{6}}Ternary‑like Operation
Although Go lacks a ternary operator, the library provides a helper to achieve similar behavior:
result := lo.Ternary[string](true, "a", "b") // result = "a"
result := lo.Ternary[string](false, "a", "b") // result = "b"
// Output: a
// bConclusion
This short guide presented the Go generic utility library github.com/samber/lo , showing how to install it and use its most common functions for type conversion, concurrency, containment, filtering, deduplication, grouping, chunking, and ternary‑like logic, making generic programming in Go more convenient.
IT Services Circle
Delivering cutting-edge internet insights and practical learning resources. We're a passionate and principled IT media platform.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.