码迷,mamicode.com
首页 > 其他好文 > 详细

Maps in Go

时间:2020-01-05 13:36:13      阅读:76      评论:0      收藏:0      [点我收藏+]

标签:cal   wan   ons   address   key   strong   ice   span   eal   

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

标签:cal   wan   ons   address   key   strong   ice   span   eal   

原文地址:https://www.cnblogs.com/drvongoosewing/p/12152007.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!