nestedmap is a nested map for Go.
nestedmap prevents panic: assignment to entry in nil map and we don't have to worry about map initialization.
nestedmap requires Go 1.18+.
$ go get github.com/arkuchy/nestedmap
package main
import (
"fmt""github.com/arkuchy/nestedmap"
)
funcmain() {
// Initialize nestedmap with type argument// map[string]map[int]boolnm:=nestedmap.NewNestedMap[string, int, bool]()
// Set valuenm.Set("gopher", 117, false)
nm.Set("gopher", 118, true)
// map[gopher:map[117:false 118:true]]]// Get outer value. The type is map[int]boolifm, ok:=nm.GetOuter("gopher"); ok {
fmt.Println(m) // output: map[117:true 118:true]
}
// Get inner value. The type is boolifv, ok:=nm.GetInner("gopher", 118); ok {
fmt.Println(v) // output: true
}
// When we try to get non-existing data, ok is falseifv, ok:=nm.GetInner("gooopher", 118); ok {
fmt.Println(v) // not called
}
}