Skip to content
Node: _.pick(obj, keys)github.com/sahilkhaire/gox/maputil

Pick

Overview

Pick returns a new map with only the given keys (lodash pick).

If you are coming from Node.js, the closest pattern is _.pick(obj, keys).

Signature

go
func Pick[K comparable, V any](m map[K]V, keys ...K) map[K]V

Compare: Node.js · Standard Go · gox

js
const subset = _.pick(obj, ['a', 'b']);
go
subset := map[string]any{}
for _, k := range []string{"a", "b"} {
    if v, ok := obj[k]; ok {
        subset[k] = v
    }
}
go
import "github.com/sahilkhaire/gox/maputil"

subset := maputil.Pick(obj, "a", "b")

Example

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

subset := maputil.Pick(obj, "a", "b")

Tips

Import github.com/sahilkhaire/gox/maputil and call Pick directly. See the comparison below for the standard library equivalent.

Standard library alternative

Use the standard library directly:

go
subset := map[string]any{}
for _, k := range []string{"a", "b"} {
    if v, ok := obj[k]; ok {
        subset[k] = v
    }
}

Back to maputil package overview

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