A small collection of generic Go utility functions.
- Go 1.26.3 or later
go get github.com/orashus/go-utilsImport the package in your Go module:
import gu "github.com/orashus/go-utils"Map transforms each element of a slice using a callback and returns a new slice with the results. The callback receives the element value and its index.
package main
import (
"fmt"
"strconv"
gu "github.com/orashus/go-utils"
)
func main() {
nums := []int{1, 2, 3, 4}
strs := gu.Map(nums, func(n int, i int) string {
return strconv.Itoa(n * i)
})
fmt.Println(strs) // [0 2 6 12]
}| Function | Description |
|---|---|
Map[T, V](arr []T, fn func(T, int) V) []V |
Applies fn to each element of arr and returns a new slice of transformed values. |