This question already has an answer here:
- Create a map of Lists 3 answers
I want to create dict in go language, but its value contains lists
dict= {
"A" : ["1", "2"],
"B" : ["3", "4"]
}
How can we create the same in go?
</div>
This question already has an answer here:
I want to create dict in go language, but its value contains lists
dict= {
"A" : ["1", "2"],
"B" : ["3", "4"]
}
How can we create the same in go?
</div>
You can create a map of string to slice of strings:
func main() {
m := make(map[string][]string)
// Each string in the m maps to a string slice
m["A"] = []string{"1", "2"}
m["B"] = []string{"3", "4"}
fmt.Println(m["A"])
// Adding to the list of a particular key
m["A"] = append(m["A"], "10")
// Creating a new key can be done similarly
m["C"] = append(m["C"], "100")
fmt.Printf("%+v
", m)
fmt.Printf("m[\"C\"] = %#v
", m["C"]) // m["C"] = []string{"100"}
}