Go-Maps
来源:互联网 发布:movidius 知乎 编辑:程序博客网 时间:2024/05/19 19:30
语法汇总
前面介绍的array、slice都是顺序性列表,本节的map则是无序的。
这个map和C/C++/Java的map一样,在Python中称为字典/dictionary。但Golang中map的用法更符合脚本语言的特点,和Python很像。
涉及的主要语法点:
var the_map map[string]intthe_map := make(map[string]int)the_map := map[string]int {key:value, ...}value, ok := the_map[key]
示例-基本用法
下面这个例子给出了map的声明方法。但通常并不这么使用,因为这种声明之后必须要调用make()初始化之后才可赋值,与其这样,不如直接:= make()这种方式。
package main/*D:\examples>go run helloworld.gopanic: assignment to entry in nil mapgoroutine 1 [running]:panic(0x45a540, 0xc04203a000) C:/Go/src/runtime/panic.go:500 +0x1afmain.main() D:/examples/helloworld.go:13 +0x6fexit status 2D:\examples>*/func main() { //x is a map of strings to ints. -- Reference: <<Introducing Go>> Slice, Map, P38 var x map[string]int x["first"] = 1}
示例-make()
package mainimport "fmt"/*D:\examples>go run helloworld.gox: 1 2D:\examples>*/func main() { x := make(map[string]int) x["first"] = 1 x["second"] = 2 debug_map(x, "x:")}func debug_map(the_map map[string]int, msg string) { fmt.Print(msg, "\t") for _, item := range the_map { fmt.Print(item, "\t") } fmt.Println()}
示例-make与初始化列表
这里所谓“初始化列表”借用C++的initialization list。在make的同时,给map指定key:value列表。
package mainimport "fmt"/*D:\examples>go run helloworld.gox: 1 2D:\examples>*/func main() { x := map[string]int { "first" : 1, "second" : 2, } debug_map(x, "x:")}func debug_map(the_map map[string]int, msg string) { fmt.Print(msg, "\t") for _, item := range the_map { fmt.Print(item, "\t") } fmt.Println()}
示例-判断元素是否存在
即便key不存在,调用the_map[the_key]的时候也不会抛出运行时异常。这和编译型语言、以及脚本语言Python都不一样。Go的处理方式更为优雅,写的代码行数也少。
package mainimport "fmt"/*D:\examples>go run helloworld.goa: 1b: 0value: 1 , exist: trueExistvalue: 0 , exist: falseNot exist.D:\examples>*/func main() { x := map[string]int { "first" : 1, "second" : 2, } a := x["first"] b := x["third"] fmt.Println("a: ", a) fmt.Println("b: ", b) find_map(x, "first") find_map(x, "fourth")}func debug_map(the_map map[string]int, msg string) { fmt.Print(msg, "\t") for _, item := range the_map { fmt.Print(item, "\t") } fmt.Println()}func find_map(the_map map[string]int, key string) { value, exist := the_map[key] fmt.Println("value: ", value, ", exist: ", exist) if exist { fmt.Println("Exist") } else { fmt.Println("Not exist.") }}
value
map的value可以是任何的数据,比如value本身也可以是map。这是Introducing Go中给出的例子,这里不再展开。
删除元素&判断是否存在
package mainimport "fmt"func main() { values := make(map[int]string) values[1] = "One" values[2] = "two" values[3] = "three" //values: map[1:One 2:two 3:three] fmt.Println("values:", values) delete(values, 2) //values: map[1:One 3:three] fmt.Println("values:", values) value, found := values[2] //value: , found: false fmt.Println("value:", value, ", found:", found)}
0 0
- Go-Maps
- go语言Exercise: Maps
- Go by Example: Maps
- 《Effective Go》--Maps
- go语言Arrays、Maps、Slices操作
- maps
- maps
- Maps
- Go!
- GO
- GO !
- go
- Go
- Go.
- go
- GO!
- go
- Go
- TCP流量控制
- 关于添中ISampleGrabber滤波器实现单帧抓图的问题
- Mysql高性能索引的策略
- Android原生SQLite的一些使用介绍
- poj1661 Help Jimmy-dp动态规划
- Go-Maps
- 几种常用的设计模式
- WebBrowser介绍——Javascript与C++互操作
- leetcode:数学:Integer Break(343)
- 关于Android动画的一点愚见
- Swift 属性观察 略谈
- Linux 套接字编程中 bind 错误:bind fail:Address already in use 解决方法
- Delphi 通过字符串实例化类
- 不同版本的Spring与Redis集群配置