我想用go语言创建dict,但它的值包含列表
dict= {
"A" : ["1", "2"],
"B" : ["3", "4"]
}
我们如何在go中创建相同的内容?
答案 0 :(得分:5)
您可以创建字符串到字符串片段的映射:
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\n", m)
fmt.Printf("m[\"C\"] = %#v\n", m["C"]) // m["C"] = []string{"100"}
}