英文:
map of map of structs and "assignment to entry in nil map"
问题
这里有一个例子,每次都会出现assignment to entry in nil map
的错误:
https://play.golang.org/p/LudJs0rVbs
为了演示我想要做的事情,这里有一个简单的版本,它会导致对数据库进行两次查找(你需要在第11行使用你的想象力):
https://play.golang.org/p/YZNFeMHyMs
基本上,我想要做的是:
things := make(map[string]map[string][]Struct)
...
stuff, there := things["first key"]
if !there {
things["first key"] = getAMapOfStringToStructs()
}
doSomethingWith(things["first key"])
我已经查看了更多关于maps-of-maps的简单示例,但似乎无法将其应用到我的问题上。
英文:
Here's an example where I get assignment to entry in nil map
every time:
https://play.golang.org/p/LudJs0rVbs
To demonstrate what I'm trying to do, here's a naive version that causes 2 lookups to the database (you'll have to use your imagination on line 11):
https://play.golang.org/p/YZNFeMHyMs
Basically, I'm trying to do this:
things := make(map[string]map[string][]Struct)
...
stuff, there := things["first key"]
if !there {
things["first key"] = getAMapOfStringToStructs()
}
doSomethingWith(things["first key"])
I've looked at the more trivial examples of maps-of-maps here but I can't seem to map that to my problem.
答案1
得分: 2
你从未在allEntries
映射上执行过make
操作:
allEntries = make(map[string]map[string][]Thing)
https://play.golang.org/p/ecdUU30FQT
英文:
You never did make
on your allEntries
map:
allEntries = make(map[string]map[string][]Thing)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论