GO, map keys have all been updated

WBOY
Release: 2024-02-09 08:36:15
forward
1167 people have browsed it

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!

Question content

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)
Copy after login

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]]

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]]

Solution

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)
Copy after login

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!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!