The map type https://golang.org/ref/spec#Map_types:
映射是一种类型(称为元素类型)的无序元素组,由另一种类型(称为键类型)的一组唯一键进行索引。
映射类型必须具有特定的值类型和特定的键类型。你想要的不符合这个条件:你想要一个值是的地图有时另一张地图(相同类型),以及有时 it's a bool
.
您的选择:
1. 使用包装值类型
这里的想法是不只是使用简单的 (bool
)值类型,但是一个包含两个潜在值的包装器:两个map
和简单值 (bool
):
type Value struct {
Children MapType
V bool
}
type MapType map[int]*Value
var m MapType
这基本上就是user3591723建议的,所以我不会进一步详细说明。
2. 有一棵树
这是 #1 的变体,但通过这种方式我们可以清楚地传达它是一棵树。
实现层次结构的最简洁的方法是使用树,其中的节点可能如下所示:
type KeyType int
type ValueType string
type Node struct {
Children map[KeyType]*Node
Value ValueType
}
这样做的好处是您可以选择值类型(即bool
在你的情况下,但你可以将其更改为任何类型 - 我用过string
用于演示)。
为了轻松构建/管理您的树,我们可以向我们的树添加一些方法Node
type:
func (n *Node) Add(key KeyType, v ValueType) {
if n.Children == nil {
n.Children = map[KeyType]*Node{}
}
n.Children[key] = &Node{Value: v}
}
func (n *Node) Get(keys ...KeyType) *Node {
for _, key := range keys {
n = n.Children[key]
}
return n
}
func (n *Node) Set(v ValueType, keys ...KeyType) {
n = n.Get(keys...)
n.Value = v
}
并使用它:1.构建一棵树,2.查询一些值,3.更改一个值:
root := &Node{Value: "root"}
root.Add(0, "first")
root.Get(0).Add(9, "second")
root.Get(0, 9).Add(3, "third")
root.Get(0).Add(4, "fourth")
fmt.Println(root)
fmt.Println(root.Get(0, 9, 3))
fmt.Println(root.Get(0, 4))
root.Set("fourthMod", 0, 4)
fmt.Println(root.Get(0, 4))
输出(尝试一下去游乐场 http://play.golang.org/p/2wVN_N-_q1):
&{map[0:0x104382f0] root}
&{map[] third}
&{map[] fourth}
&{map[] fourthMod}
3. 使用递归类型定义
这可能令人惊讶,但可以定义一个map
使用递归定义输入具有无限或动态“深度”的 Go:
type X map[int]X
正如它所说:这是一个map
with int
与映射本身类型相同的键和值。
这种递归类型的一大缺点是它不能在值类型中存储任何“有用”的数据。它只能存储是否存在与某个值相同的值的“事实”bool
类似信息(bool
type: true
or false
),在极少数情况下这可能就足够了,但在大多数情况下却不够。
让我们看一个构建“树”的示例:
var x X
x = map[int]X{}
x[0] = map[int]X{}
x[0][9] = map[int]X{}
x[0][9][3] = map[int]X{}
x[0][4] = map[int]X{}
fmt.Println(x)
Output:
map[0:map[9:map[3:map[]] 4:map[]]]
如果我们想测试是否存在基于一系列键的“值”,我们有两个选择:要么使用特殊的v, ok := m[i]
索引(报告指定键的值是否存在),或测试该值是否不存在nil
, e.g. m[i] != nil
.
让我们看一些测试上面构建的地图的示例:
var ok bool
_, ok = x[0][9][3]
fmt.Println("x[0][9][3] exists:", ok, "; alternative way:", x[0][9][3] != nil)
_, ok = x[0][9][4]
fmt.Println("x[0][9][4] exists:", ok, "; alternative way:", x[0][9][4] != nil)
_, ok = x[0][4]
fmt.Println("x[0][4] exists:", ok, "; alternative way:", x[0][4] != nil)
_, ok = x[0][4][9][9][9]
fmt.Println("x[0][4][9][9][9] exists:", ok, "; alternative way:", x[0][4][9][9][9] != nil)
Output:
x[0][9][3] exists: true ; alternative way: true
x[0][9][4] exists: false ; alternative way: false
x[0][4] exists: true ; alternative way: true
x[0][4][9][9][9] exists: false ; alternative way: false
尝试这些去游乐场 http://play.golang.org/p/qzHfuZDLar.
Note:虽然x[0][4]
是最后一个“叶子”,索引进一步像x[0][4][9][9][9]
不会引起恐慌nil
映射可以被索引并产生值类型的零值(即nil
如果值类型是映射类型)。