Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
go-创建值为列表的字典_Go - Fatal编程技术网

go-创建值为列表的字典

go-创建值为列表的字典,go,Go,我想用go语言创建dict,但它的值包含列表 dict= { "A" : ["1", "2"], "B" : ["3", "4"] } 如何在go中创建相同的字符串?您可以创建字符串到字符串切片的映射: func main() { m := make(map[string][]string) // Each string in the m maps to a string slice m["A"] = []string{"1", "2"} m["B"] =

我想用go语言创建dict,但它的值包含列表

dict= {
"A" : ["1", "2"],
"B" : ["3", "4"]
}

如何在go中创建相同的字符串?

您可以创建字符串到字符串切片的映射:

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"}
}