Home >Backend Development >Golang >GO, map keys have all been updated
php Xiaobian Yuzai’s latest news: All GO map keys have been updated! GO Map Key is a powerful navigation application that provides users with accurate and convenient map navigation services. After the latest update, the GO map key has added more functions and optimizations, allowing users to more easily obtain the navigation information they need. Whether you are traveling, traveling or looking for nearby services, GO Map Key can provide you with all-round help. Whether you are traveling by car or walking, the GO map key can plan the optimal route and provide real-time navigation guidance for you. No matter where you are, the GO map key is your right assistant when traveling!
Is there something wrong with my approach? I don't understand why all map keys are updated, it should just be the "ether" key.
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)
Go to the amusement park
The output will be
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]]
p>
But I'm looking forward to it
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]]
p>
That’s because mappings are actually like pointers All "wlan", "ether", bridge" and "wg" keys point to the same value (rxTx). There are many ways to achieve what you want, but here's a quick solution:
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) rxTxWlan := map[string]int{"rx": 0, "tx": 0} rxTxEther := map[string]int{"rx": 0, "tx": 0} rxTxBridge := map[string]int{"rx": 0, "tx": 0} rxTxWg := map[string]int{"rx": 0, "tx": 0} typeMap := map[string]map[string]int{"wlan": rxTxWlan, "ether": rxTxEther, "bridge": rxTxBridge, "wg": rxTxWg} fmt.Println(typeMap) for _, v := range dst { if v["type"] == "ether" { typeMap["ether"]["rx"] += 1 typeMap["ether"]["tx"] += 1 } } fmt.Println(typeMap)
The above is the detailed content of GO, map keys have all been updated. For more information, please follow other related articles on the PHP Chinese website!