前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布

map

作者头像
李海彬
发布2018-03-26 14:35:48
7080
发布2018-03-26 14:35:48
举报
文章被收录于专栏:Golang语言社区
// _Maps_ are Go's built-in [associative data type](http://en.wikipedia.org/wiki/Associative_array)

// (sometimes called _hashes_ or _dicts_ in other languages).

package main

import "fmt"

func main() {

// To create an empty map, use the builtin `make`:

// `make(map[key-type]val-type)`.

m := make(map[string]int)

// Set key/value pairs using typical `name[key] = val`

// syntax.

m["k1"] = 7

m["k2"] = 13

// Printing a map with e.g. `Println` will show all of

// its key/value pairs.

fmt.Println("map:", m)

// Get a value for a key with `name[key]`.

v1 := m["k1"]

fmt.Println("v1: ", v1)

// The builtin `len` returns the number of key/value

// pairs when called on a map.

fmt.Println("len:", len(m))

// The builtin `delete` removes key/value pairs from

// a map.

delete(m, "k2")

fmt.Println("map:", m)

// The optional second return value when getting a

// value from a map indicates if the key was present

// in the map. This can be used to disambiguate

// between missing keys and keys with zero values

// like `0` or `""`.

_, prs := m["k2"]

fmt.Println("prs:", prs)

// You can also declare and initialize a new map in

// the same line with this syntax.

n := map[string]int{"foo": 1, "bar": 2}

fmt.Println("map:", n)

}

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2017-04-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Golang语言社区 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档