I have a code that looks like this:
var c [][]string
c = append(c, d)
c = append(c, l)
Assuming that both d and l are []strings. This works, however it would return something like this:
[["0241025570","0241025571","1102182000"],["0241025570","0241025571","1102182000"]]
How would it be possible to structure it to look like this:
["d": ["0241025570","0241025571","1102182000"], "l":["0241025570","0241025571","1102182000"]]
What you would like to have would no longer be a slice, but a map of slices. You can get the desired results using the following code:
c := make(map[string][]string)
c["d"] = d
c["l"] = l
Depending on your usage, you may want to make copies of d
and l
, instead of using them directly in the map:
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)