Easier way to handle the nested data structure
MIT License
Easier way to handle the nested data structure
package main
import (
"fmt"
"github.com/knqyf263/nested"
)
func main() {
n := nested.Nested{}
n.Set([]string{"a", "b"}, 1)
n.SetByString("a.c.d", ".", "test")
n.SetByString("/e/f", "/", true)
var result interface{}
result, _ = n.Get([]string{"a", "c", "d"})
fmt.Println(result)
// Output: test
result, _ = n.GetByString("e/f", "/")
fmt.Println(result)
// Output: true
var b int
b, _ = n.GetInt([]string{"a", "b"})
fmt.Println(b)
// Output: 1
n.Delete([]string{"a", "c"})
}
func main() {
n := nested.Nested{}
n.Set([]string{"a", "b", "c"}, 1)
n.Set([]string{"d", "e"}, 2)
n.SetByString("f.g.h", ".", false)
walkFn := func(keys []string, value interface{}) error {
key := strings.Join(keys, ".")
// Skip all keys under "f"
if key == "f" {
return nested.SkipKey
}
fmt.Println(key, value)
return nil
}
n.Walk(walkFn)
// Output:
// a map[b:map[c:1]]
// a.b map[c:1]
// a.b.c 1
// d map[e:2]
// d.e 2
// f map[g:map[h:1]]
}
$ go get github.com/knqyf263/nested
go get github.com/knqyf263/nested
MIT
Teppei Fukuda