Go – map set

Time:2021-3-4

summary

A map set is an unordered key value data structure.

The key / value in the map set can be of any type, but all keys must belong to the same data type, all values must belong to the same data type, and the data types of key and value can be different.
Declaration map

 

   //demo_14.go    
    package main    
    import (    
        "fmt"    
    )    
    func main() {    
        var p1 map[int]string    
        p1 = make(map[int]string)    
        p1[1] = "Tom"    
        fmt.Println("p1 :", p1)    
        var p2 map[int]string = map[int]string{}    
        p2[1] = "Tom"    
        fmt.Println("p2 :", p2)    
        var p3 map[int]string = make(map[int]string)    
        p3[1] = "Tom"    
        fmt.Println("p3 :", p3)    
        p4 := map[int]string{}    
        p4[1] = "Tom"    
        fmt.Println("p4 :", p4)    
        p5 := make(map[int]string)    
        p5[1] = "Tom"    
        fmt.Println("p5 :", p5)    
        p6 := map[int]string{    
            1 : "Tom",    
        }    
        fmt.Println("p6 :", p6)    
    }

 

Results of operation:

Generating JSON

 

//demo_15.go    
    package main    
    import (    
        "encoding/json"    
        "fmt"    
    )    
    func main() {    
        res := make(map[string]interface{})    
        res["code"] = 200    
        res["msg"]  = "success"    
        res["data"] = map[string]interface{}{    
            "username" : "Tom",    
            "age"      : "30",    
            "Hobby": [] string {"reading", "mountain climbing"},    
        }    
        fmt.Println("map data :", res)    
        //Serialization    
        jsons, errs := json.Marshal(res)    
        if errs != nil {    
            fmt.Println("json marshal error:", errs)    
        }    
        fmt.Println("")    
        fmt.Println("--- map to json ---")    
        fmt.Println("json data :", string(jsons))    
        //Deserialization    
        res2 := make(map[string]interface{})    
        errs = json.Unmarshal([]byte(jsons), &res2)    
        if errs != nil {    
            fmt.Println("json marshal error:", errs)    
        }    
        fmt.Println("")    
        fmt.Println("--- json to map ---")    
        fmt.Println("map data :", res2)    
    }

 

Results of operation:

Edit and delete

 

  //demo_16.go    
    package main    
    import (    
        "fmt"    
    )    
    func main() {    
        person := map[int]string{    
            1 : "Tom",    
            2 : "Aaron",    
            3 : "John",    
        }    
        fmt.Println("data :",person)    
        delete(person, 2)    
        fmt.Println("data :",person)    
        person[2] = "Jack"    
        person[3] = "Kevin"    
        fmt.Println("data :",person)    
    }

 

Results of operation:

Recommended Today

Quickly use the latest 15 common APIs of vue3

Before that, I wrote a blog to introduce the new features of vue3. I had a brief understanding of the features of vue3, and at the end of the article, I gave you a little experience in vue3Compsition APISimple use of Address of last article: follow Youda’s steps and experience the new features of vue3 […]