Go exercise 2-map operations

Source: Internet
Author: User
func T2_1() {
// Key value string, value int type
    m1 := map[string]int{}
// Add an element
    m1["str1"] = 1
    fmt.Println(m1)
// Directly Overwrite
    m1["str1"] = 2
    fmt.Println(m1)
If V, OK: = m1 ["str1"]; OK {// determine whether the key exists.
        fmt.Println(v)
    }
FMT. println (m1 ["C"]) // for keys that do not exist, \ 0 is returned directly without error.
Delete (M1, "str1") // delete an element
 
    fmt.Println(m1)
 
// Run the make function in advance? A reasonable element quantity parameter helps improve performance.
// Because of preapproval? I? Large memory to avoid frequent expansion during subsequent operations.
    m2 := make(map[string]int, 1000)
    m2["str2"] = 200
    fmt.Println(m2)
// Len returns the number of actually stored elements. CAP does not work.
    fmt.Println(len(m2))
}

Go exercise 2-map operations

Related Article

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.