创建字符串映射到列表 [英] Create a map of string to List
本文介绍了创建字符串映射到列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想创建一个字符串映射到container/list.List
实例.这是正确的解决方法吗?
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)
}
推荐答案
每当我想使用List
时,我都发现切片是正确的选择,例如
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])
}
这篇关于创建字符串映射到列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文