Golang Basic--map Dictionary

Source: Internet
Author: User

Map similar to Dict (dictionary type) in Python language to store data in key-value form

    • Key must be a == != type that supports or compares operations, and cannot be a function, map, or slice
    • Map lookup is much faster than linear search, but 100 times times slower than using indexed access data
    • Map uses the make () function to create := a simple notation that supports assignment

      //基本操作func main() {    var m map[int]string //定义一个map变量    m = map[int]string{}        //第一种方式    m = make(map[int]string)  //第二种方式:使用make    m := make(map[int]string) //第三种方式make直接初始化    m[1] = "ok"               //赋值一对key-value    a := m[1]                 //获取一个key对应的value值    delete(m, 1)              //删除map中的某一个key            fmt.Println(m)    fmt.Println(m)    fmt.Println(a)    fmt.Println(m)}/*outputmap_init---->   map[]map_add---->    map[1:ok]map_get---->    okmap_del---->    map[]*/

Nested map: A nested dictionary similar to Python, with values that require layers of progression when assigned, and the need to perform make initialization on the inner map each time you assign a value.

    • Sample code

      package mainimport "fmt"func main() {    var m map[int]map[int]string     //嵌套字典    m = make(map[int]map[int]string) //初始化map--最外层    m[1] = make(map[int]string)      //make初始化嵌套字典,第一个键对应的map类型    m[1][1] = "ok"                   //key1_value1赋值    b, ok := m[1][1]                 //使用多返回值,第二值返回值时bool类型,如果前一个有返回值,返回true,反之返回false    fmt.Println(b, ok)    fmt.Println(m)    a, ok := m[2][1] //由于key2,对应的value2赋值前没有make初始化,所以,    fmt.Println(a, ok)    if !ok {        m[2] = make(map[int]string)    }    m[2][1] = "Good"    a, ok = m[2][1]    fmt.Println(a, ok)    fmt.Println(m)}/*输出a, ok--->   ok truem --->      map[1:map[1:ok]]a, ok--->        falsea, ok--->   Good true           //初始化后*/

For iterative operations, similar to the python,dict operation, for K,v in DIC.

    • Sample code

      //for 迭代操作slicepackage mainimport "fmt"func main() {    sum := make([]map[int]string, 5) //初始化一个slice,元素是map类型    for i := range sum {        sum[i] = make(map[int]string, 1)        sum[i][1] = "ok"        fmt.Println(sum[i])    }    fmt.Println(sum)}/*输出i=0--->     map[1:ok]i=1--->     map[1:ok]i=2--->     map[1:ok]i=3--->     map[1:ok]i=4--->     map[1:ok]sum--->     [map[1:ok] map[1:ok] map[1:ok] map[1:ok] map[1:ok]]*/

Map, indirect sort, by slice, the key is sorted.

    • Sample code

      //间接排序,对key进行排序package mainimport "fmt"import "sort" //排序包func main() {    m := map[int]string{1: "a", 2: "b", 3: "c", 4: "d"}    //这是一个map类型    s := make([]int, len(m))                                        //这是一个slice类型    i := 0    for k := range m {        s[i] = k        i ++    }    sort.Ints(s)    fmt.Println(s)}/*输出[1 2 3 4]*/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.