- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我可以通过
创建一个“静态” maptype m map[int]map[int]map[int]bool
但是“键”的长度是动态的:
|---unknown len--|
m[1][2][3][4][2][0] = true
或
|---unk len--|
m[1][2][3][4] = true
如何在 Go 中创建此 map ?或者存在任何方式?
补充:分层重要
提前致谢!
最佳答案
map type :
A map is an unordered group of elements of one type, called the element type, indexed by a set of unique keys of another type, called the key type.
映射类型必须具有特定的值类型和特定的键类型。你想要的不符合这个条件:你想要一个 map ,其中的值是有时另一个 map (相同类型),有时它是一个bool
.
您的选择:
这里的想法是不要只使用一个简单的 (bool
) 值类型,而是使用一个包装器来保存您的两个潜在值:map
和简单的值(bool
):
type Value struct {
Children MapType
V bool
}
type MapType map[int]*Value
var m MapType
这基本上是 user3591723 的建议,所以我不会进一步详细说明。
这是 #1 的变体,但通过这种方式我们清楚地表明它是一棵树。
实现层次结构的最简洁的方法是使用树,其中的节点可能如下所示:
type KeyType int
type ValueType string
type Node struct {
Children map[KeyType]*Node
Value ValueType
}
这样做的好处是您可以选择值类型(在您的情况下为 bool
,但您可以将其更改为任何类型 - 我使用 string
进行演示).
为了轻松构建/管理您的树,我们可以向我们的 Node
类型添加一些方法:
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))
输出(在 Go Playground 上尝试):
&{map[0:0x104382f0] root}
&{map[] third}
&{map[] fourth}
&{map[] fourthMod}
这可能令人惊讶,但可以使用递归定义在 Go 中定义具有无限或动态“深度”的 map
类型:
type X map[int]X
如其名:它是一个带有 int
键的 map
,以及与 map 本身类型相同的值。
这种递归类型的一大缺点是它不能在值类型中存储任何“有用”的数据。它只能存储“事实”是否存在与 bool
类信息相同的值(bool
类型:true
或 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)
输出:
map[0:map[9:map[3:map[]] 4:map[]]]
如果我们想测试是否存在基于一系列键的“值”,我们有两个选择:要么使用特殊的 v, ok := m[i]
索引(报告指定键的值是否存在),或测试该值是否不是 nil
,例如m[i] != nil
.
让我们看一些测试上面构建的 map 的例子:
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)
输出:
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
在 Go Playground 上试试这些.
注意:即使 x[0][4]
是最后的“叶子”,索引更像 x[0][4][9 ][9][9]
不会导致 panic ,因为 nil
映射可以被索引并产生值类型的零值(即 nil
如果值类型是映射类型)。
关于dictionary - 戈朗 : How to create unknown (dynamic) Map length,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34761129/
这是我的代码 14 20 {"Confirm Email"} 21 在第 17 行我得到错误 Type '{ pathname: string; user: { em
这是我的代码 14 20 {"Confirm Email"} 21 在第 17 行我得到错误 Type '{ pathname: string; user: { em
这个问题已经有答案了: How do I compare strings in Java? (23 个回答) 已关闭 8 年前。 为什么 KeyEvent.getKeyText(0).substrin
我正在尝试 Rust 的新 wasm32-unknown-unknown 目标,我在调用数学函数(例如 sin、cos、exp、atan2)时遇到问题。 cargo .toml: [package]
当我为 spring-boot 创建启动项目时,我在 pom 文件中收到此错误。这只是为了创建一个基本的 Spring Boot 项目 Project build error: Invalid pac
我已经订阅了我想要传输的数据。但不知何故它不起作用。我收到此错误: The property pipe is not available for type "OperatorFunction" 这是我
运行以下查询时。select * from surgerys where to_char(dt_surgery ,'DD-MM-YYYY' ) = to_char('12-02-2012','DD-M
我在运行存储过程时遇到以下异常: com.microsoft.sqlserver.jdbc.SQLServerException:不支持从 UNKNOWN 到 UNKNOWN 的转换。 过程定义如下:
我尝试运行以下代码。顺便说一句,我对 python 和 sklearn 都是新手。 import pandas as pd import numpy as np from sklearn.linear
我已经阅读了关于未知类型的官方文档,但我很难真正理解它是如何工作的。 人们可以在文档中读到:“在没有首先断言或缩小到更具体的类型之前,不允许对未知进行任何操作。” 但如果我有这个功能: const f
我正在尝试在Mac OS中设置Hadoop 2.6.0 我正在关注这篇文章: http://hadoop.apache.org/docs/r2.4.0/hadoop-project-dist/hado
配置 Nexus docker 注册表和代理“dockerhub-proxy”后,如下所述: https://help.sonatype.com/repomanager3/formats/docker
我收到此错误 - “ValueError:未知标签类型:'unknown'” 我已经在网上搜索但无法摆脱这个错误,顺便说一句,我是 python 的新手:) 我的数据有 5 行 22 列,最后一列是标
使用 SHA256 摘要标识符 拉取图像失败 最佳答案 不幸的是,这是 DockerHub 删除 Docker 1.9 守护进程的向后兼容性的副作用。当使用 Docker 1.10 推送图像时,较旧的
我是 postgresql 的新手,正在尝试使用全文搜索 to_tsvector但是我遇到了错误。 SQL 和错误 SELECT to_tsvector('english', 'The quick b
每当我这样做时 npm run watch ,第一次编译工作正常 - 但经过几次编译后,我最终会得到这个错误: 95% emitting unnamed compat pluginError: UNK
在一个新的 Angular 应用程序中,我收到以下错误:Error from chokidar : Error: UNKNOWN: unknown error, watch我已经删除并重新安装 nod
使用 Typescipt 4.x.x 我写了一些代码来实现其他语言 Elm/Rust/Haskell 中常用的 Maybe/Option 类型。 我想写一个可以接受映射类型的通用函数 type MyM
const submitted = useSelector((state) => state.post.submitted) 对于上面的状态。我得到错误: (参数)状态:未知对象的类型为“未知”。 这
我正在尝试将多架构 docker 镜像推送到 docker hub 并遇到错误(在 https://github.com/docker/distribution/issues/3100 处打开了 do
我是一名优秀的程序员,十分优秀!