Insert or update an element in map m:

  1. m[key] = elem

Retrieve an element:

  1. elem = m[key]

Delete an element:

  1. delete(m, key)

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

  1. 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.

  1. package main
  2. import "fmt"
  3.  
  4. func main() {
  5. m := make(map[string]int)
  6.  
  7. m["Answer"] =
  8. fmt.Println("The value:", m["Answer"])
  9.  
  10. m["Answer"] =
  11. fmt.Println("The value:", m["Answer"])
  12.  
  13. v, ok := m["Answer"]
  14. fmt.Println("The value:", v, "Present?", ok)
  15.  
  16. delete(m, "Answer")
  17. fmt.Println("The value:", m["Answer"])
  18.  
  19. v2, ok2 := m["Answer"]
  20. fmt.Println("The value:", v2, "Present?", ok2)
  21. }

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

A Tour of Go Mutating Maps的更多相关文章

  1. A Tour of Go Exercise: Maps

    Implement WordCount. It should return a map of the counts of each “word” in the string s. The wc.Tes ...

  2. Essential controls for web app

    AUTO-COMPLETE/AUTO-SUGGEST Auto-complete using Vaadin Offer auto-suggest or auto-complete to help yo ...

  3. A Tour of Go Maps

    A map maps keys to values. Maps must be created with make (not new) before use; the nil map is empty ...

  4. exercise.tour.go google的go官方教程答案

    /* Exercise: Loops and Functions #43 */ package main import ( "fmt" "math" ) fun ...

  5. go官网教程A Tour of Go

    http://tour.golang.org/#1 中文版:http://go-tour-cn.appsp0t.com/#4 package main import ( "fmt" ...

  6. Go structs、slices、maps

    [Go structs.slices.maps] 1.定义时*在变量名后面,使用时*在变量名前面. 2.定义struct,type在前,struct关键字在后. 3.指针可以指定struct. 4.A ...

  7. 动态规划:HDU1224-Free DIY Tour

       Free DIY Tour Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  8. Hdu 3488 Tour (KM 有向环覆盖)

    题目链接: Hdu 3488 Tour 题目描述: 有n个节点,m条有权单向路,要求用一个或者多个环覆盖所有的节点.每个节点只能出现在一个环中,每个环中至少有两个节点.问最小边权花费为多少? 解题思路 ...

  9. 检索Google Maps地图位置(小训练)

    名称:检索地图位置 内容:地图初期显示和检索显示 功能:根据条件检索地图的经度与纬度 1.在这之前我们需要创建一个表(Accoun__c),添加一个重要的字段地理位置情報,它会默认的给你两个字段经度和 ...

随机推荐

  1. hdu 2509 Be the Winner 博弈论

    博弈论水题!!! 代码如下: #include<stdio.h> #include<iostream> using namespace std; int main(){ int ...

  2. linux 修改命令行编码 乱码解决方案

    修改/etc/default/locale命令:sudo vim /etc/default/locale1将下面这两行 LANG=zh_CN.UTF-8 LANGUAGE=zh_CN:zh 修改为: ...

  3. hibernate annotation注解 主键ID自增长

    @Id @SequenceGenerator(name="increment") @GeneratedValue(strategy=GenerationType.AUTO, gen ...

  4. linux中class_create和class_register说明

    http://blog.csdn.net/angle_birds/article/details/16802099 本文介绍linux中class_create和class_register的相关使用 ...

  5. 封装SqlHelper

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.C ...

  6. SSL构建单双向https认证

    1.  SSL基本介绍 我们常常在使用网上银行时看到的连接都是以“https”开始的,那么这个https是什么呢?这其实是表示目前连接使用了SSL进加密,能保证客户端到服务器端的通信都在被保护起来,那 ...

  7. 分布式事务的管理--atomikos

    在一些业务场景及技术架构下,跨库的事务时不可避免的,这时候如何统一管理事务,保证事务的强一致性是整个系统稳定.可用基石.一些中间件如tuxedo.cics就是凭借这个能力占据了金融.电信.银行等很大的 ...

  8. 利用循环removeChild删除节点只删除一半问题

    <!DOCTYPE html> <html>   <head>     <title>adduser.html</title>        ...

  9. 【ZOJ】3609 Modular Inverse

    1. 题目描述求乘法逆元. 2. 基本思路利用扩展gcd求逆元,模板题目. 3. 代码 /* 3609 */ #include <iostream> #include <sstrea ...

  10. NPOI的版本查看

    从github上clone源代码 git clone https://github.com/tonyqus/npoi.git 下载的版本库中,有一个名为Release Notes.txt的文件,在这个 ...