I'd like to create a map of string to container/list.List
instances. Is this the correct way to go about it?
package main import ( "fmt" "container/list" ) func main() { x := make(map[string]*list.List) x["key"] = list.New() x["key"].PushBack("value") fmt.Println(x["key"].Front().Value) }
Whenever I've wanted to use a List
I've found that a slice was the right choice, eg
package main import "fmt" func main() { x := make(map[string][]string) x["key"] = append(x["key"], "value") x["key"] = append(x["key"], "value1") fmt.Println(x["key"][0]) fmt.Println(x["key"][1]) }
My favorite syntax for declaring a map of string to slice of string:
mapOfSlices := map[string][]string{ "first": {}, "second": []string{"one", "two", "three", "four", "five"}, "third": []string{"quarter", "half"}, }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With