Skip to content

A small Go utility to walk potentially nested maps using the visitor pattern

License

Notifications You must be signed in to change notification settings

affanshahid/walkmap

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Walkmap

A Go library to iterate over potentially nested map keys using the visitor pattern

Installing

go get -u github.com/affanshahid/walkmap

Usage

package main

import (
	"reflect"

	"github.com/affanshahid/walkmap"
)

func main() {
	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,
			},
		},
	}

	walkmap.Walk(data, func(keyPath []interface{}, value interface{}, kind reflect.Kind) {
		// keyPath is the slice of keys used to arrive at the current node
		// value is the current node's value
		// kind describes the nature of the value, gotten using `reflect.TypeOf(value).Kind()`
		//...
	})
}

About

A small Go utility to walk potentially nested maps using the visitor pattern

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages