问题:
我在另一个结构中有一个结构映射,我想初始化结构的嵌套映射,但显然这是不可能的。
代码:
type Exporter struct {
TopicsByName map[string]Topic
}
type Topic struct {
Name string
Partitions map[int32]Partition
}
type Partition struct {
PartitionID int32
HighWaterMark int64
}
// Eventually I want to do something like:
e := Exporter{ TopicsByName: make(map[string]Topic) }
for _, topicName := range topicNames {
// This does not work because "cannot assign to struct field e.TopicsByName[topicName].Partitions in map"
e.TopicsByName[topicName].Partitions = make(map[int32]Partition)
}
// I wanted to initialize all these maps so that I can do
e.TopicsByName[x.TopicName].Partitions[x.PartitionID] = Partition{...}
我不明白为什么我不能初始化上面的嵌套结构映射。嵌套以结构体为值的地图有那么糟糕吗?我怎样才能解决这个问题?
蛊毒传说
有只小跳蛙
Helenr
相关分类