【转】Go maps in action
原文: https://blog.golang.org/go-maps-in-action
--------------------------------------------------------------------------------------------------------------
Introduction
One of the most useful data structures in computer science is the hash table. Many hash table implementations exist with varying properties, but in general they offer fast lookups, adds, and deletes. Go provides a built-in map type that implements a hash table.
Declaration and initialization
A Go map type looks like this:
map[KeyType]ValueType
where KeyType
may be any type that is comparable (more on this later), and ValueType
may be any type at all, including another map!
This variable m
is a map of string keys to int values:
var m map[string]int
Map types are reference types, like pointers or slices, and so the value of m
above is nil
; it doesn't point to an initialized map. A nil map behaves like an empty map when reading, but attempts to write to a nil map will cause a runtime panic; don't do that. To initialize a map, use the built in make
function:
m = make(map[string]int)
The make
function allocates and initializes a hash map data structure and returns a map value that points to it. The specifics of that data structure are an implementation detail of the runtime and are not specified by the language itself. In this article we will focus on the use of maps, not their implementation.
Working with maps
Go provides a familiar syntax for working with maps. This statement sets the key "route"
to the value 66
:
m["route"] = 66
This statement retrieves the value stored under the key "route"
and assigns it to a new variable i:
i := m["route"]
If the requested key doesn't exist, we get the value type's zero value. In this case the value type is int
, so the zero value is 0
:
j := m["root"]
// j == 0
The built in len
function returns on the number of items in a map:
n := len(m)
The built in delete
function removes an entry from the map:
delete(m, "route")
The delete
function doesn't return anything, and will do nothing if the specified key doesn't exist.
A two-value assignment tests for the existence of a key:
i, ok := m["route"]
In this statement, the first value (i
) is assigned the value stored under the key "route"
. If that key doesn't exist, i
is the value type's zero value (0
). The second value (ok
) is a bool
that is true
if the key exists in the map, and false
if not.
To test for a key without retrieving the value, use an underscore in place of the first value:
_, ok := m["route"]
To iterate over the contents of a map, use the range
keyword:
for key, value := range m {
fmt.Println("Key:", key, "Value:", value)
}
To initialize a map with some data, use a map literal:
commits := map[string]int{
"rsc": 3711,
"r": 2138,
"gri": 1908,
"adg": 912,
}
The same syntax may be used to initialize an empty map, which is functionally identical to using the make
function:
m = map[string]int{}
Exploiting zero values
It can be convenient that a map retrieval yields a zero value when the key is not present.
For instance, a map of boolean values can be used as a set-like data structure (recall that the zero value for the boolean type is false). This example traverses a linked list of Nodes
and prints their values. It uses a map of Node
pointers to detect cycles in the list.
type Node struct {
Next *Node
Value interface{}
}
var first *Node visited := make(map[*Node]bool)
for n := first; n != nil; n = n.Next {
if visited[n] {
fmt.Println("cycle detected")
break
}
visited[n] = true
fmt.Println(n.Value)
}
The expression visited[n]
is true
if n
has been visited, or false
if n
is not present. There's no need to use the two-value form to test for the presence of n
in the map; the zero value default does it for us.
Another instance of helpful zero values is a map of slices. Appending to a nil slice just allocates a new slice, so it's a one-liner to append a value to a map of slices; there's no need to check if the key exists. In the following example, the slice people is populated with Person
values. Each Person
has a Name
and a slice of Likes. The example creates a map to associate each like with a slice of people that like it.
type Person struct {
Name string
Likes []string
}
var people []*Person likes := make(map[string][]*Person)
for _, p := range people {
for _, l := range p.Likes {
likes[l] = append(likes[l], p)
}
}
To print a list of people who like cheese:
for _, p := range likes["cheese"] {
fmt.Println(p.Name, "likes cheese.")
}
To print the number of people who like bacon:
fmt.Println(len(likes["bacon"]), "people like bacon.")
Note that since both range and len treat a nil slice as a zero-length slice, these last two examples will work even if nobody likes cheese or bacon (however unlikely that may be).
Key types
As mentioned earlier, map keys may be of any type that is comparable. The language spec defines this precisely, but in short, comparable types are boolean, numeric, string, pointer, channel, and interface types, and structs or arrays that contain only those types. Notably absent from the list are slices, maps, and functions; these types cannot be compared using ==
, and may not be used as map keys.
It's obvious that strings, ints, and other basic types should be available as map keys, but perhaps unexpected are struct keys. Struct can be used to key data by multiple dimensions. For example, this map of maps could be used to tally web page hits by country:
hits := make(map[string]map[string]int)
This is map of string to (map of string
to int
). Each key of the outer map is the path to a web page with its own inner map. Each inner map key is a two-letter country code. This expression retrieves the number of times an Australian has loaded the documentation page:
n := hits["/doc/"]["au"]
Unfortunately, this approach becomes unwieldy when adding data, as for any given outer key you must check if the inner map exists, and create it if needed:
func add(m map[string]map[string]int, path, country string) {
mm, ok := m[path]
if !ok {
mm = make(map[string]int)
m[path] = mm
}
mm[country]++
}
add(hits, "/doc/", "au")
On the other hand, a design that uses a single map with a struct key does away with all that complexity:
type Key struct {
Path, Country string
}
hits := make(map[Key]int)
When an Vietnamese person visits the home page, incrementing (and possibly creating) the appropriate counter is a one-liner:
hits[Key{"/", "vn"}]++
And it's similarly straightforward to see how many Swiss people have read the spec:
n := hits[Key{"/ref/spec", "ch"}]
Concurrency
Maps are not safe for concurrent use: it's not defined what happens when you read and write to them simultaneously. If you need to read from and write to a map from concurrently executing goroutines, the accesses must be mediated by some kind of synchronization mechanism. One common way to protect maps is with sync.RWMutex.
This statement declares a counter
variable that is an anonymous struct containing a map and an embedded sync.RWMutex
.
var counter = struct{
sync.RWMutex
m map[string]int
}{m: make(map[string]int)}
To read from the counter, take the read lock:
counter.RLock()
n := counter.m["some_key"]
counter.RUnlock()
fmt.Println("some_key:", n)
To write to the counter, take the write lock:
counter.Lock()
counter.m["some_key"]++
counter.Unlock()
Iteration order
When iterating over a map with a range loop, the iteration order is not specified and is not guaranteed to be the same from one iteration to the next. Since the release of Go 1.0, the runtime has randomized map iteration order. Programmers had begun to rely on the stable iteration order of early versions of Go, which varied between implementations, leading to portability bugs. If you require a stable iteration order you must maintain a separate data structure that specifies that order. This example uses a separate sorted slice of keys to print a map[int]string
in key order:
import "sort" var m map[int]string
var keys []int
for k := range m {
keys = append(keys, k)
}
sort.Ints(keys)
for _, k := range keys {
fmt.Println("Key:", k, "Value:", m[k])
}
By Andrew Gerrand
【转】Go maps in action的更多相关文章
- 【UI插件】简单的日历插件(下)—— 学习MVC思想
前言 我们上次写了一个简单的日历插件,但是只是一个半成品,而且做完后发现一些问题,于是我们今天尝试来解决这些问题 PS:距离上次貌似很久了 上次,我们大概遇到哪些问题呢: ① 既然想做一套UI库,那么 ...
- Thinkphp源码分析系列(四)–Dispatcher类
下面我们来分析一下Thinkphp中的url解析和路由调度类.此类主要功能是 // +--------------------------------------------------------- ...
- 【iScroll源码学习03】iScroll事件机制与滚动条的实现
前言 想不到又到周末了,周末的时间要抓紧学习才行,前几天我们学习了iScroll几点基础知识: 1. [iScroll源码学习02]分解iScroll三个核心事件点 2. [iScroll源码学习01 ...
- 【再探backbone 01】模型-Model
前言 点保存时候不注意发出来了,有需要的朋友将就看吧,还在更新...... 几个月前学习了一下backbone,这段时间也用了下,感觉之前对backbone的学习很是基础,前几天有个园友问我如何将路由 ...
- Backbone事件模块源码分析
事件模块Backbone.Events在Backbone中占有十分重要的位置,其他模块Model,Collection,View所有事件模块都依赖它.通过继承Events的方法来实现事件的管理,可以说 ...
- Backbone笔记(续)
Backbone Bockbone 总览 Backbone 与 MVC 模式:解决某一类问题的通用方案 - 套路 MVC:一种架构模式,解耦代码,分离关注点 M(Model) - 数据模型 V(Vie ...
- BACKBONE源代码解析
//2014.11// Backbone.js 1.0.0 // (c) 2010-2013 Jeremy Ashkenas, DocumentCloud Inc. // Backbone may b ...
- Backbone Events 源码笔记
用了backbone一段时间了,做一些笔记和总结,看的源码是1.12 backbone有events,model,collection,histoty,router,view这些模块,其中events ...
- The Go Programming Language. Notes.
Contents Tutorial Hello, World Command-Line Arguments Finding Duplicate Lines A Web Server Loose End ...
随机推荐
- 架构设计之NodeJS操作消息队列RabbitMQ
一. 什么是消息队列? 消息(Message)是指在应用间传送的数据.消息可以非常简单,比如只包含文本字符串,也可以更复杂,可能包含嵌入对象. 消息队列(Message Queue)是一种应用间的通信 ...
- web.config 数据库连接
方法一:connectionsStrings 首先配置web.config文件 <configurations> <connectionStrings> <add nam ...
- POJ 1200 Crazy Search【Hash入门】
RK法:https://www.cnblogs.com/16crow/p/6879988.html #include<cstdio> #include<string> #inc ...
- gwy常识
其实公务员考试是一门艺术,七分靠水平,三分凭发挥,充分而又细致的准备则是取得优秀成绩的前提.考生若想在笔试中成功上岸,还需苦练内功,凭技巧和真才实学在考场上一较高下.那么针对历年上海公务员考试笔试考情 ...
- ANY和SOME 运算符
在SQL中ANY和SOME是同义词,所以下面介绍的时候只使用ANY,SOME的用法和功能和ANY一模一样.和IN运算符不同,ANY必须和其他的比较运算符共同使用,而且必须将比较运算符放在ANY 关键字 ...
- uva 10648(简单dp)
Recently one of my friend Tarik became a member of the food committee of an ACM regional competition ...
- js实现table导出为Excel文件
1.首先创建好表格. 2.然后在js中写三个方法 1)判断浏览器 2)定义文档类型 template : 定义文档的类型,相当于html页面中顶部的<!DOCTYPE> 声明.(个人理解, ...
- [BZOJ4003][JLOI2015]城池攻占(左偏树)
这题有多种做法,一种是倍增预处理出每个点往上走2^i步最少需要的初始战斗力,一种是裸的启发式合并带标记splay. 每个点合并能攻占其儿子的所有骑士,删去所有无法攻占这个城市的骑士并记录答案. 注意到 ...
- [UOJ347]通道
锟题x1 以下用$d_k(x,y)$表示$x,y$在第树$k$上的距离,$h_k(x)$表示$x$在树$k$上的深度 先做两棵树,即最大化$d_1(x,y)+d_2(x,y)=h_1(x)+h_1(y ...
- 【计算几何】【凸包】Gym - 101164H - Pub crawl
平面上n个点,点之间沿直线走,规划一条路线,每次只能往左半平面的点走,走过最多的点. 显然所有的点都能走过. n^2的暴力显然是每次找左边与其所形成夹角最小的点,但这样过不了(卡常数?). 或者每轮不 ...