Skip to content
Node: arr.map(fn)github.com/sahilkhaire/gox/slice

Map

Overview ​

Transforms each element of a slice, returning a new slice — identical mental model to Array.prototype.map. Uses Go generics so input and output types can differ safely.

If you are coming from Node.js, the closest pattern is arr.map(fn).

Signature ​

go
func Map[T, U any](in []T, fn func(T) U) []U

Compare: Node.js · Standard Go · gox ​

js
const names = users.map(u => u.name);
go
names := make([]string, len(users))
for i, u := range users {
    names[i] = u.Name
}
go
import "github.com/sahilkhaire/gox/slice"

names := slice.Map(users, func(u User) string { return u.Name })

Example ​

go
import "github.com/sahilkhaire/gox/slice"

names := slice.Map(users, func(u User) string { return u.Name })

Tips ​

Chain with slice.Filter and slice.Reduce for lodash-style pipelines. Pre-allocates output slice for performance.

Standard library alternative ​

Use the standard library directly:

go
names := make([]string, len(users))
for i, u := range users {
    names[i] = u.Name
}

← Back to slice package overview

MIT Licensed · Built for Node.js developers moving to Go