创建列表的Golang地图(Create a Golang map of Lists)
我想创建一个container/list.List实例的映射。 这是正确的方式去解决它吗?
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) }I'd like to create a map of 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) }最满意答案
每当我想要使用List我发现一个切片是正确的选择,例如
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]) }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]) }创建列表的Golang地图(Create a Golang map of Lists)我想创建一个container/list.List实例的映射。 这是正确的方式去解决它吗?
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) }I'd like to create a map of 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) }最满意答案
每当我想要使用List我发现一个切片是正确的选择,例如
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]) }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]) }
发布评论