Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Walk ¶
func Walk(m interface{}, visitor Visitor)
Walk walks the given map `m` and calls the visitor at every non-map node
Example ¶
data := map[interface{}]interface{}{
"1": 1,
"2": 2,
"3": 3,
"nested": map[interface{}]interface{}{
"4": 4,
"5": 5,
"6": 6,
"evenMoreNested": map[interface{}]interface{}{
"7": 7,
"8": 8,
"9": 9,
},
},
}
concatenatePath := func(paths []interface{}) string {
strPaths := make([]string, 0, len(paths))
for _, path := range paths {
strPaths = append(strPaths, path.(string))
}
return strings.Join(strPaths, ".")
}
flattened := map[string]interface{}{}
paths := []string{}
Walk(data, func(keyPath []interface{}, value interface{}, kind reflect.Kind) {
pathStr := concatenatePath(keyPath)
paths = append(paths, pathStr)
flattened[pathStr] = value
})
sort.Strings(paths)
for _, p := range paths {
fmt.Printf("Key: %s, Value: %d\n", p, flattened[p])
}
Output: Key: 1, Value: 1 Key: 2, Value: 2 Key: 3, Value: 3 Key: nested.4, Value: 4 Key: nested.5, Value: 5 Key: nested.6, Value: 6 Key: nested.evenMoreNested.7, Value: 7 Key: nested.evenMoreNested.8, Value: 8 Key: nested.evenMoreNested.9, Value: 9
Types ¶
Click to show internal directories.
Click to hide internal directories.