创建列表的Golang地图 [英] Create a Golang map of Lists

查看:81
本文介绍了创建列表的Golang地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个 container / list.List 实例的映射。这是正确的方式吗?

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 时,我发现一个切片是正确的选择,例如

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地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆