3 回答

TA貢獻1895條經驗 獲得超3個贊
無法分配給映射值中的字段。解決方法是將結構值分配給映射值:
for _, topicName := range []string{"a"} { e.TopicsByName[topicName] = Topic{Partitions: make(map[int32]Partition)} }

TA貢獻1824條經驗 獲得超8個贊
您可以像預期的那樣初始化它:
e := Exporter{
TopicsByName: map[string]Topic{
"my-topic": Topic{
Name: "my-topic",
Partitions: map[int32]Partition{
int32(1): Partition{
PartitionID: int32(1),
HighWaterMark: int64(199),
},
},
},
},
}
這不是直接回答問題,因為您想遍歷主題列表,但如果在 kafka 測試中使用它,我強烈建議使用上面更詳細/文字的格式。
https://play.golang.org/p/zm3A7CIvbyu

TA貢獻1780條經驗 獲得超4個贊
如果您知道初始值。為什么不這樣做
val := `
{
"topics_by_name": {
"name1": {
"name":"topicname1",
"partions": {
1: {
"partion_id":1,
"high_water_mark":12,
},
2: {}
}
},
"name2": {}
}
}
`
func main() {
var m map[string]interface{}
// if m has be well designed, use your designed map is better
// init
json.Unmarshal(val, &m)
}
- 3 回答
- 0 關注
- 174 瀏覽
添加回答
舉報