热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

MapsinGo

IntroductionInthisarticlewewilltalkabout:Maps-Whatarethey?-Howtocreatamap?-Howtomanipulate

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


推荐阅读
  • 知识图谱——机器大脑中的知识库
    本文介绍了知识图谱在机器大脑中的应用,以及搜索引擎在知识图谱方面的发展。以谷歌知识图谱为例,说明了知识图谱的智能化特点。通过搜索引擎用户可以获取更加智能化的答案,如搜索关键词"Marie Curie",会得到居里夫人的详细信息以及与之相关的历史人物。知识图谱的出现引起了搜索引擎行业的变革,不仅美国的微软必应,中国的百度、搜狗等搜索引擎公司也纷纷推出了自己的知识图谱。 ... [详细]
  • 本文介绍了lua语言中闭包的特性及其在模式匹配、日期处理、编译和模块化等方面的应用。lua中的闭包是严格遵循词法定界的第一类值,函数可以作为变量自由传递,也可以作为参数传递给其他函数。这些特性使得lua语言具有极大的灵活性,为程序开发带来了便利。 ... [详细]
  • 本文介绍了使用Java实现大数乘法的分治算法,包括输入数据的处理、普通大数乘法的结果和Karatsuba大数乘法的结果。通过改变long类型可以适应不同范围的大数乘法计算。 ... [详细]
  • HDU 2372 El Dorado(DP)的最长上升子序列长度求解方法
    本文介绍了解决HDU 2372 El Dorado问题的一种动态规划方法,通过循环k的方式求解最长上升子序列的长度。具体实现过程包括初始化dp数组、读取数列、计算最长上升子序列长度等步骤。 ... [详细]
  • 本文讨论了如何优化解决hdu 1003 java题目的动态规划方法,通过分析加法规则和最大和的性质,提出了一种优化的思路。具体方法是,当从1加到n为负时,即sum(1,n)sum(n,s),可以继续加法计算。同时,还考虑了两种特殊情况:都是负数的情况和有0的情况。最后,通过使用Scanner类来获取输入数据。 ... [详细]
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
  • 开发笔记:加密&json&StringIO模块&BytesIO模块
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了加密&json&StringIO模块&BytesIO模块相关的知识,希望对你有一定的参考价值。一、加密加密 ... [详细]
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • 本文介绍了C#中数据集DataSet对象的使用及相关方法详解,包括DataSet对象的概述、与数据关系对象的互联、Rows集合和Columns集合的组成,以及DataSet对象常用的方法之一——Merge方法的使用。通过本文的阅读,读者可以了解到DataSet对象在C#中的重要性和使用方法。 ... [详细]
  • Mac OS 升级到11.2.2 Eclipse打不开了,报错Failed to create the Java Virtual Machine
    本文介绍了在Mac OS升级到11.2.2版本后,使用Eclipse打开时出现报错Failed to create the Java Virtual Machine的问题,并提供了解决方法。 ... [详细]
  • 在说Hibernate映射前,我们先来了解下对象关系映射ORM。ORM的实现思想就是将关系数据库中表的数据映射成对象,以对象的形式展现。这样开发人员就可以把对数据库的操作转化为对 ... [详细]
  • javascript  – 概述在Firefox上无法正常工作
    我试图提出一些自定义大纲,以达到一些Web可访问性建议.但我不能用Firefox制作.这就是它在Chrome上的外观:而那个图标实际上是一个锚点.在Firefox上,它只概述了整个 ... [详细]
  • 本文介绍了在SpringBoot中集成thymeleaf前端模版的配置步骤,包括在application.properties配置文件中添加thymeleaf的配置信息,引入thymeleaf的jar包,以及创建PageController并添加index方法。 ... [详细]
  • 本文详细介绍了Linux中进程控制块PCBtask_struct结构体的结构和作用,包括进程状态、进程号、待处理信号、进程地址空间、调度标志、锁深度、基本时间片、调度策略以及内存管理信息等方面的内容。阅读本文可以更加深入地了解Linux进程管理的原理和机制。 ... [详细]
  • 1,关于死锁的理解死锁,我们可以简单的理解为是两个线程同时使用同一资源,两个线程又得不到相应的资源而造成永无相互等待的情况。 2,模拟死锁背景介绍:我们创建一个朋友 ... [详细]
author-avatar
安彬2502936127
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有