Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a map of string to List

Tags:

go

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) } 
like image 853
Carson Avatar asked Oct 01 '12 17:10

Carson


2 Answers

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]) } 
like image 90
Nick Craig-Wood Avatar answered Sep 29 '22 12:09

Nick Craig-Wood


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"}, } 
like image 25
Sam Houston Avatar answered Sep 29 '22 14:09

Sam Houston