我正在做的事情有什么问题吗?我不明白为什么所有地图键都被更新,它应该只是“以太”键。
data := []byte(`
[{".id":"*1","actual-mtu":"1500","default-name":"ether1","disabled":"false","l2mtu":"1514","type":"ether"},{".id":"*2","actual-mtu":"1500","default-name":"bridge2","disabled":"false","l2mtu":"1514","type":"bridge"}]
`)
var dst []map[string]string
json.Unmarshal(data, &dst)
rxTx := map[string]int{"rx": 0, "tx": 0}
typeMap := map[string]map[string]int{"wlan": rxTx, "ether": rxTx, "bridge": rxTx, "wg": rxTx}
fmt.Println(typeMap)
for _, v := range dst {
if v["type"] == "ether" {
typeMap["ether"]["rx"] += 1
typeMap["ether"]["tx"] += 1
}
}
fmt.Println(typeMap)
输出将是
map[bridge:map[rx:1 tx:1] ether:map[rx:1 tx:1] wg:map[rx:1 tx:1] wlan:map[rx:1 tx:1]]
但我期待着
map[bridge:map[rx:0 tx:0] ether:map[rx:1 tx:1] wg:map[rx:0 tx:0] wlan:map[rx:0 tx:0]]