Maps in Go

Posted drvongoosewing

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Maps in Go相关的知识,希望对你有一定的参考价值。

Introduction

In this article we will talk about:

Maps

- What are they?

- How to creat a map?

- How to manipulate a map?

 

Maps

1. What are they?

Map is one of the collection types in Go. In addition to Slice/ Array, map has another dimention called key.

It works like a real world dictionary.

package main

import (
	"fmt"
)

func main() {
	statePops := map[string]int{
		"California": 39250017,
		"Texas": 27862596,
		"Florida": 20612439,
	}
	fmt.Println(statePops)
}
// output
map[California:39250017 Florida:20612439 Texas:27862596]

Most of types in Go can be key of map, except Slices/ Maps/ Functions.

2. How to create a map?

a) Literal syntax

We can create a map by literal syntax(like above example). We just write out everything we need.

If we want to create an empty map, we can literal write: statepops := map[string]int{}

b) by make() function

We can create a slice by make() function. We can also create a map by make() function.

Especially when a map is key-value is later create in a for-loop, and we don‘t want to initialize a map at it‘s creation.

Not like in creating a Slice we have three arguments in make() function: length and capacity.

We only use one argument in creating a map. And the addinonal argument seems not working here.

package main

import (
	"fmt"
)

func main() {
	statePops := make(map[string]int, 2)
	fmt.Println(statePops, len(statePops))
	
	statePops["California"] = 39250017
	fmt.Println(statePops, len(statePops))
	
	statePops["Texas"] = 27862596
	fmt.Println(statePops, len(statePops))
	
	statePops["Florida"] = 20612439
	fmt.Println(statePops, len(statePops))
}
// output
map[] 0
map[California:39250017] 1
map[California:39250017 Texas:27862596] 2
map[California:39250017 Florida:20612439 Texas:27862596] 3

3. How to manipulate a map?

a) Pull out a value: by brackets and it‘s key  

For example, in first map example, we can use: statePops["California"]

b) Add an element to a map: by brackets and it‘s key 

in first map example, we can use: statePops["Ohio"] = 11614373

c) Element‘s order: not guarantee

Even if in difiniton we write one elemnt before another, their order is not guarantee.

d) Delete an element: use build-in function delete()

This function has no return value. We will use it standalone.

package main

import (
	"fmt"
)

func main() {
	statePops := map[string]int{
		"California": 39250017,
		"Texas": 27862596,
		"Florida": 20612439,
	}
	fmt.Println(statePops)
	
	delete(statePops, "Texas")
	fmt.Println(statePops)
}
// output
map[California:39250017 Florida:20612439 Texas:27862596]
map[California:39250017 Florida:20612439]

e) If a key doesn‘t exist: will return zero

Actually map will return two values: value of the key you appointed and bool of the key‘s exist.

package main

import (
	"fmt"
)

func main() {
	statePops := map[string]int{
		"California": 39250017,
		"Texas": 27862596,
		"Florida": 20612439,
	}
	fmt.Println(statePops["Ohio"])

	val, ok := statePops["Ohio"]
	fmt.Println(val, ok)
}

f) Equal operation between two maps: share same memory address

If we manipulate one map, the result will also reflect on other one.

package main

import (
	"fmt"
)

func main() {
	statePops := map[string]int{
		"California": 39250017,
		"Texas": 27862596,
		"Florida": 20612439,
	}
	sp := statePops
	delete(sp, "Texas")
	fmt.Println(sp)
	fmt.Println(statePops)
}
// output
map[California:39250017 Florida:20612439]
map[California:39250017 Florida:20612439]

  

以上是关于Maps in Go的主要内容,如果未能解决你的问题,请参考以下文章

Android Google Maps in Fragment Clustering Bug

RecyclerView holder中的Android Google Maps动态片段

golang 片段7 for https://medium.com/@francesc/why-are-there-nil-channels-in-go-9877cc0b2308

[12 Go语言基础-Maps]

010_go语言中的maps映射(字典)

GO 智能合约cannot use transactionRecordId + strconv.Itoa(id) (type string) as type byte in append(示例代码(代