create a map with a key of type map in go

To create a map with a key of type map in Go, you can use nested maps. Here's an example:

main.go
// create a map with key of type map[string]int
nestedMap := make(map[string]map[string]int)

// add values to the nested map
nestedMap["map1"] = make(map[string]int)
nestedMap["map1"]["key1"] = 1
nestedMap["map2"] = make(map[string]int)
nestedMap["map2"]["key2"] = 2

// access the values of the nested map
fmt.Println(nestedMap["map1"]["key1"]) // output: 1
fmt.Println(nestedMap["map2"]["key2"]) // output: 2
412 chars
13 lines

In this example, we create a map (nestedMap) with a key of type map[string]int using the make function. Then, we can add values to this nested map by initializing the nested maps and assigning values to their keys. Finally, we can access the values of the nested map by using the appropriate keys.

gistlibby LogSnag