具有字符串索引的多级切片

multilevel slices with string indexes

我的代码如下所示:

var c [][]string

c = append(c, d)
c = append(c, l)

假设d和l都是[]字符串。这行得通,但是它会 return 像这样:

[["0241025570","0241025571","1102182000"],["0241025570","0241025571","1102182000"]]

如何将它构造成如下所示:

["d": ["0241025570","0241025571","1102182000"], "l":["0241025570","0241025571","1102182000"]]

您想要的不再是切片,而是切片图。您可以使用以下代码获得所需的结果:

c := make(map[string][]string)
c["d"] = d
c["l"] = l

根据您的使用情况,您可能希望 make copies of dl,而不是直接在地图中使用它们:

c := make(map[string][]string)
c["d"] = make([]string, len(d))
c["l"] = make([]string, len(l))
copy(c["d"], d)
copy(c["l"], l)