乐趣区

关于golang:golang-map基础知识

map 初始化的四种办法

    var myGreeting map[string]string
    fmt.Println(myGreeting)        // map[]
    fmt.Println(myGreeting == nil) //true

    myGreeting2 := make(map[string]string)
    myGreeting2["Tim"] = "Good morning."
    myGreeting2["Jenny"] = "Bonjour."

    fmt.Println(myGreeting2)         //map[Jenny:Bonjour. Tim:Good morning.]

    myGreeting3 := map[string]string{}
    myGreeting3["Tim"] = "Good morning."
    myGreeting3["Jenny"] = "Bonjour."
    fmt.Println(myGreeting3)         //map[Jenny:Bonjour. Tim:Good morning.]

    myGreeting4 := map[string]string{
        "Time":  "Good morning.",
        "Jenny": "Bonjour.",
    }

    fmt.Println(myGreeting4)          //map[Jenny:Bonjour. Time:Good morning.]
    fmt.Println(myGreeting4["Jenny"]) //Bonjour.

map 增加操作

    myGreeting := map[string]string{
        "Tim":   "Good morning!",
        "Jenny": "Bonjour!",
    }

    myGreeting["Harleen"] = "Howdy"

    fmt.Println(myGreeting)

len 获取 map 的长度

    myGreeting := map[string]string{
        "Tim":   "Good morning!",
        "Jenny": "Bonjour!",
    }

    myGreeting["Harleen"] = "Howdy"

    fmt.Println(len(myGreeting))

更新 map

    myGreeting := map[string]string{
        "Tim":   "Good morning!",
        "Jenny": "Bonjour!",
    }

    myGreeting["Harleen"] = "Howdy"
    fmt.Println(myGreeting)
    myGreeting["Harleen"] = "Gidday"
    fmt.Println(myGreeting)

删除 map 的值:

    myGreeting := map[string]string{
        "zero":  "Good morning!",
        "one":   "Bonjour!",
        "two":   "Buenos dias!",
        "three": "Bongiorno!",
    }

    fmt.Println(myGreeting)
    delete(myGreeting, "two")
    fmt.Println(myGreeting)
    // ------------------------------------------------------------
        myGreeting := map[int]string{
        0: "Good morning!",
        1: "Bonjour!",
        2: "Buenos dias!",
        3: "Bongiorno!",
    }

    fmt.Println(myGreeting)
    delete(myGreeting, 7)
    fmt.Println(myGreeting)

判断 key 是否存在

    myGreeting := map[int]string{
        0: "Good morning!",
        1: "Bonjour!",
        2: "Buenos dias!",
        3: "Bongiorno!",
    }

    fmt.Println(myGreeting)

    // delete(myGreeting, 2)

    if val, exists := myGreeting[2]; exists {fmt.Println("That value exists.")
        fmt.Println("val:", val)
        fmt.Println("exists:", exists)
    } else {fmt.Println("That value doesn't exist.")
        fmt.Println("val:", val)
        fmt.Println("exists:", exists)
    }

    fmt.Println(myGreeting)
scoreMap := make(map[string]int)
    scoreMap["张三"] = 90
    scoreMap["小明"] = 100
    // 如果 key 存在 ok 为 true,v 为对应的值;不存在 ok 为 false,v 为值类型的零值
    v, ok := scoreMap["张三"]
    if ok {fmt.Println(v)
    } else {fmt.Println("查无此人")
    }

map 的遍历

    myGreeting := map[int]string{
        0: "Good morning!",
        1: "Bonjour!",
        2: "Buenos dias!",
        3: "Bongiorno!",
    }

    for key, val := range myGreeting {fmt.Println(key, "-", val)
    }
}
   scoreMap := make(map[string]int)
    scoreMap["张三"] = 90
    scoreMap["小明"] = 100
    scoreMap["王五"] = 60
    for k := range scoreMap {fmt.Println(k)
    }
退出移动版