结构体的映射的映射和“对空映射中的条目进行赋值”。

huangapple go评论101阅读模式
英文:

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)

https://play.golang.org/p/ecdUU30FQT

huangapple
  • 本文由 发表于 2015年5月15日 01:27:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/30243581.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定