首页 > 其他 > 详细

A Tour of Go Mutating Maps

时间:2014-10-28 00:37:13      阅读:258      评论:0      收藏:0      [点我收藏+]

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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!