Go基礎-map

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。// xuhh_go_map project
/*
1.map的特點:
    a.map是go語言的內建參考型別,所以多個map指向同一個底層的情況下,一個值發生變化,全部發生變化
    b.map是無序的,每次迭代的順序都是不確定的。
    c.map只有 len 沒有 cap。
    d.map不是安全執行緒的,在多個go-routine中使用的時候,要加鎖。
    e.map[key]value
        key 必須支援 運算子(== 、!= )的操作,例如 number/string/pointer/array/struct/interface
        value 可以是任意類型。
2.map的建立:
    a. new 進行建立
        mapA := new(map[int]string)
        *mapA = map[int]string{}
        (*mapA)[112] = "112"
        (*mapA)[110] = "110"
    b.直接聲明建立
        var map1 map[int]string = map[int]string{}
        map2 := map[int]string{
            11: "11",
            12: "12",
            13: "13",
        }
    c.make進行建立
        map3 := make(map[int]string, 1000) // 1000 是map的容量 ,這點和slice是一樣的,預先申請一塊記憶體,避免以後頻繁擴張,提升效能
3.map 的一些基本操作
    a.判斷索引值是否存在
        if v, ok := map1[5]; ok == true {
            fmt.Println("this key is exit ", v)
        } else {
            fmt.Println("this key is not exit ")
        }
    b.增加
        map[key] = value
    c.修改
        map 在修改value的時候
            如果value是實值型別,要想改變值,需要全部替換(value 換 value)
            如果是參考型別,則可以直接賦值替換
        mapS := map[string]PERSON{}
        mapS["xuhh"] = PERSON{name: "xuhh", old: 26}
        mapS["xuhh"].name = "xuhehe" // error  cannot assign to mapS["xuhh"].name
    d.刪除
        可以在迭代的時候安全刪除索引值對
        delete(map, key)
    e.迭代遍曆
        for k, v := range map {
            fmt.Println("key = ", k, "value = ", v)
        }
*/
package mainimport (    "fmt")type PERSON struct {    name string    old  int}func main() {    //new 進行建立    mapA := new(map[int]string)    *mapA = map[int]string{}    (*mapA)[112] = "112"    (*mapA)[110] = "110"    for v := range *mapA {        fmt.Println(v)    }    //直接建立,可以直接初始化:    var map1 map[int]string = map[int]string{}    map2 := map[int]string{        11: "11",        12: "12",        13: "13",    }    fmt.Println(map2)    map1[2] = "222"    map1[3] = "333"    map1[1] = "111"    map1[6] = "666"    map1[8] = "888"    map1[5] = "555"    fmt.Println(map1)    // make 建立    map3 := make(map[int]string, 1000)    map3[22] = "22"    fmt.Println(map3)    // map 的操作    // 尋找    if v, ok := map1[5]; ok == true {        fmt.Println("this key is exit ", v)    } else {        fmt.Println("this key is not exit ")    }    //add    map1[5] = "exit"    fmt.Println(map1[5])    //modify    mapS := map[string]PERSON{}    mapS["xuhh"] = PERSON{name: "xuhh", old: 26}    //mapS["xuhh"].name = "xuhehe" // error  cannot assign to mapS["xuhh"].name    //map 在操作value 的時候,如果value 是 實值型別 ,要想改變值,需要全部替換(value 換 value),如果不是參考型別,則可以直接賦值替換    //delete    delete(map1, 5)    if _, ok := map1[5]; !ok {        fmt.Println("this key is deleted")    }    //迭代遍曆    for k, v := range map1 {        fmt.Println("key = ", k, "value = ", v)    }}





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.