Get
Overview
Get reads a nested value from m using dot-separated path (lodash get).
If you are coming from Node.js, the closest pattern is _.get(obj, "a.b").
Signature
go
func Get(m map[string]any, path string) (any, bool)Compare: Node.js · Standard Go · gox
js
const city = _.get(obj, 'address.city', 'unknown');go
city := "unknown"
if addr, ok := obj["address"].(map[string]any); ok {
if c, ok := addr["city"].(string); ok {
city = c
}
}go
import "github.com/sahilkhaire/gox/maputil"
city, _ := maputil.Get[string](obj, "address.city")Example
go
import "github.com/sahilkhaire/gox/maputil"
city, _ := maputil.Get[string](obj, "address.city")Tips
Import github.com/sahilkhaire/gox/maputil and call Get directly. See the comparison below for the standard library equivalent.
Standard library alternative
Use the standard library directly:
go
city := "unknown"
if addr, ok := obj["address"].(map[string]any); ok {
if c, ok := addr["city"].(string); ok {
city = c
}
}