码迷,mamicode.com
首页 > 其他好文 > 详细

A Tour of Go Mutating Maps

时间:2014-10-28 00:39:53      阅读:243      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   ar   for   sp   div   log   bs   

Insert or update an element in map m:

m[key] = elem

Retrieve an element:

elem = m[key]

Delete an element:

delete(m, key)

Test that a key is present with a two-value assignment:

elem, ok = m[key]

If key is in mok is true. If not, ok is false and elem is the zero value for the map‘s element type.

Similarly, when reading from a map if the key is not present the result is the zero value for the map‘s element type.

 

package main 
import "fmt"

func main() {
    m := make(map[string]int)

    m["Answer"] = 42
    fmt.Println("The value:", m["Answer"])

    m["Answer"] = 48
    fmt.Println("The value:", m["Answer"])

    v, ok := m["Answer"]
    fmt.Println("The value:", v, "Present?", ok)


    delete(m, "Answer")
    fmt.Println("The value:", m["Answer"])

    v2, ok2 := m["Answer"]
    fmt.Println("The value:", v2, "Present?", ok2)
}

好变态的map用法,真不知道google是怎怎么想的

A Tour of Go Mutating Maps

标签:style   blog   color   ar   for   sp   div   log   bs   

原文地址:http://www.cnblogs.com/ghgyj/p/4055485.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!