go语言的map就相当于python的dict

1、map的初始化

  1. //创建map
  2. //k的类型是int,v的类型是string
  3. var test25_1 map[int]string
  4.  
  5. fmt.Println(test25_1)
  6. //map[]
  7.  
  8. fmt.Println(test25_1 == nil)
  9. //true
  10.  
  11. test25_2 := map[int]string{}
  12.  
  13. test25_3 := make(map[int]string)
  14.  
  15. fmt.Println(test25_2)
  16. //map[]
  17. fmt.Println(test25_3)
  18. //map[]
  19.  
  20. //初始化一个有容量的map
  21. test25_4 := make(map[int]string,10)
  22.  
  23. fmt.Println(test25_4)
  24. //map[]
  25.  
  26. //初始化map

  

2、map的初始化

  1. //定义的同时初始化
  2. var test25_5 map[int] string = map[int]string{1:"test1",2:"test2"}
  3.  
  4. fmt.Println(test25_5)
  5. //map[1:test1 2:test2]
  6.  
  7. //自动推倒类型
  8.  
  9. var test25_6 = map[int]string{3:"test3",4:"test4"}
  10. fmt.Println(test25_6)
  11. //map[3:test3 4:test4]

  

3、map的键值操作,增和改

  1. //键值操作
  2.  
  3. //修改
  4. test25_6[3] = "test333"
  5. fmt.Println(test25_6)
  6. //map[3:test333 4:test4]
  7.  
  8. //增加
  9. test25_6[5] = "test5"
  10. fmt.Println(test25_6)
  11. //map[3:test333 4:test4 5:test5]

  

4、map的遍历操作

  1. //map的遍历操作
  2. for k,v := range test25_6{
  3. fmt.Printf("k值为%d,v值为%s\n",k,v)
  4. }
  5.  
  6. //k值为5,v值为test5
  7. //k值为3,v值为test333
  8. //k值为4,v值为test
  9.  
  10. for k:= range test25_6{
  11. fmt.Printf("k值为%d,v值为%s\n",k,test25_6[k])
  12. }
  13.  
  14. //k值为3,v值为test333
  15. //k值为4,v值为test4
  16. //k值为5,v值为test5

  

5、判断map是否存在某个k

  1. //判断某个k对应的v是否存在
  2.  
  3. //value为k对应的值,ok为是否存在
  4. value,ok := test25_6[3]
  5.  
  6. fmt.Println(value,ok)
  7. //test333 true
  8. value1,ok1 := test25_6[7]
  9.  
  10. fmt.Println(value1,ok1)
  11. //false

  

6、删除map中的某个k

  1. fmt.Println(test25_6)
  2. //map[3:test333 4:test4 5:test5]
  3. delete(test25_6,3)
  4. fmt.Println(test25_6)
  5. //map[4:test4 5:test5]

  

Go 语言Map(集合)

Map 是一种无序的键值对的集合。Map 最重要的一点是通过 key 来快速检索数据,key 类似于索引,指向数据的值。

Map 是一种集合,所以我们可以像迭代数组和切片那样迭代它。不过,Map 是无序的,我们无法决定它的返回顺序,这是因为 Map 是使用 hash 表来实现的。

定义 Map

可以使用内建函数 make 也可以使用 map 关键字来定义 Map:

  1. /* 声明变量,默认 map 是 nil */
  2. var map_variable map[key_data_type]value_data_type
  3.  
  4. /* 使用 make 函数 */
  5. map_variable := make(map[key_data_type]value_data_type)

如果不初始化 map,那么就会创建一个 nil map。nil map 不能用来存放键值对

实例

下面实例演示了创建和使用map:

  1. package main
  2.  
  3. import "fmt"
  4.  
  5. func main() {
  6. var countryCapitalMap map[string]string /*创建集合 */
  7. countryCapitalMap = make(map[string]string)
  8.  
  9. /* map插入key - value对,各个国家对应的首都 */
  10. countryCapitalMap [ "France" ] = "巴黎"
  11. countryCapitalMap [ "Italy" ] = "罗马"
  12. countryCapitalMap [ "Japan" ] = "东京"
  13. countryCapitalMap [ "India " ] = "新德里"
  14.  
  15. /*使用键输出地图值 */
  16. for country := range countryCapitalMap {
  17. fmt.Println(country, "首都是", countryCapitalMap [country])
  18. }
  19.  
  20. /*查看元素在集合中是否存在 */
  21. capital, ok := countryCapitalMap [ "American" ] /*如果确定是真实的,则存在,否则不存在 */
  22. /*fmt.Println(capital) */
  23. /*fmt.Println(ok) */
  24. if (ok) {
  25. fmt.Println("American 的首都是", capital)
  26. } else {
  27. fmt.Println("American 的首都不存在")
  28. }
  29. }

以上实例运行结果为:

  1. France 首都是 巴黎
  2. Italy 首都是 罗马
  3. Japan 首都是 东京
  4. India 首都是 新德里
  5. American 的首都不存在

delete() 函数

delete() 函数用于删除集合的元素, 参数为 map 和其对应的 key。实例如下:

  1. package main
  2.  
  3. import "fmt"
  4.  
  5. func main() {
  6. /* 创建map */
  7. countryCapitalMap := map[string]string{"France": "Paris", "Italy": "Rome", "Japan": "Tokyo", "India": "New delhi"}
  8.  
  9. fmt.Println("原始地图")
  10.  
  11. /* 打印地图 */
  12. for country := range countryCapitalMap {
  13. fmt.Println(country, "首都是", countryCapitalMap [ country ])
  14. }
  15.  
  16. /*删除元素*/ delete(countryCapitalMap, "France")
  17. fmt.Println("法国条目被删除")
  18.  
  19. fmt.Println("删除元素后地图")
  20.  
  21. /*打印地图*/
  22. for country := range countryCapitalMap {
  23. fmt.Println(country, "首都是", countryCapitalMap [ country ])
  24. }
  25. }

以上实例运行结果为:

  1. 原始地图
  2. India 首都是 New delhi
  3. France 首都是 Paris
  4. Italy 首都是 Rome
  5. Japan 首都是 Tokyo
  6. 法国条目被删除
  7. 删除元素后地图
  8. Italy 首都是 Rome
  9. Japan 首都是 Tokyo
  10. India 首都是 New delhi

基于 go 实现简单 HashMap,暂未做 key 值的校验。

  1. package main
  2.  
  3. import (
  4. "fmt"
  5. )
  6.  
  7. type HashMap struct {
  8. key string
  9. value string
  10. hashCode int
  11. next *HashMap
  12. }
  13.  
  14. var table [16](*HashMap)
  15.  
  16. func initTable() {
  17. for i := range table{
  18. table[i] = &HashMap{"","",i,nil}
  19. }
  20. }
  21.  
  22. func getInstance() [16](*HashMap){
  23. if(table[0] == nil){
  24. initTable()
  25. }
  26. return table
  27. }
  28.  
  29. func genHashCode(k string) int{
  30. if len(k) == 0{
  31. return 0
  32. }
  33. var hashCode int = 0
  34. var lastIndex int = len(k) - 1
  35. for i := range k {
  36. if i == lastIndex {
  37. hashCode += int(k[i])
  38. break
  39. }
  40. hashCode += (hashCode + int(k[i]))*31
  41. }
  42. return hashCode
  43. }
  44.  
  45. func indexTable(hashCode int) int{
  46. return hashCode%16
  47. }
  48.  
  49. func indexNode(hashCode int) int {
  50. return hashCode>>4
  51. }
  52.  
  53. func put(k string, v string) string {
  54. var hashCode = genHashCode(k)
  55. var thisNode = HashMap{k,v,hashCode,nil}
  56.  
  57. var tableIndex = indexTable(hashCode)
  58. var nodeIndex = indexNode(hashCode)
  59.  
  60. var headPtr [16](*HashMap) = getInstance()
  61. var headNode = headPtr[tableIndex]
  62.  
  63. if (*headNode).key == "" {
  64. *headNode = thisNode
  65. return ""
  66. }
  67.  
  68. var lastNode *HashMap = headNode
  69. var nextNode *HashMap = (*headNode).next
  70.  
  71. for nextNode != nil && (indexNode((*nextNode).hashCode) < nodeIndex){
  72. lastNode = nextNode
  73. nextNode = (*nextNode).next
  74. }
  75. if (*lastNode).hashCode == thisNode.hashCode {
  76. var oldValue string = lastNode.value
  77. lastNode.value = thisNode.value
  78. return oldValue
  79. }
  80. if lastNode.hashCode < thisNode.hashCode {
  81. lastNode.next = &thisNode
  82. }
  83. if nextNode != nil {
  84. thisNode.next = nextNode
  85. }
  86. return ""
  87. }
  88.  
  89. func get(k string) string {
  90. var hashCode = genHashCode(k)
  91. var tableIndex = indexTable(hashCode)
  92.  
  93. var headPtr [16](*HashMap) = getInstance()
  94. var node *HashMap = headPtr[tableIndex]
  95.  
  96. if (*node).key == k{
  97. return (*node).value
  98. }
  99.  
  100. for (*node).next != nil {
  101. if k == (*node).key {
  102. return (*node).value
  103. }
  104. node = (*node).next
  105. }
  106. return ""
  107. }
  108.  
  109. //examples
  110. func main() {
  111. getInstance()
  112. put("a","a_put")
  113. put("b","b_put")
  114. fmt.Println(get("a"))
  115. fmt.Println(get("b"))
  116. put("p","p_put")
  117. fmt.Println(get("p"))
  118. }

go语言之map的更多相关文章

  1. go语言学习--map的并发

    go提供了一种叫map的数据结构,可以翻译成映射,对应于其他语言的字典.哈希表.借助map,可以定义一个键和值,然后可以从map中获取.设置和删除这个值,尤其适合数据查找的场景.但是map的使用有一定 ...

  2. GO_05:GO语言基础map与函数

    1. map 1. 类似其它语言中的哈希表活着字典,以 key-value 形式存储数据 2. key 必须是支持 == 或 != 比较运算的类型,不可以是函数.map 或 slice 3. map ...

  3. GO语言基础map与函数

    1. map 1. 类似其它语言中的哈希表活着字典,以 key-value 形式存储数据 2. key 必须是支持 == 或 != 比较运算的类型,不可以是函数.map 或 slice 3. map ...

  4. 深度解密Go语言之 map

    目录 什么是 map 为什么要用 map map 的底层如何实现 map 内存模型 创建 map 哈希函数 key 定位过程 map 的两种 get 操作 如何进行扩容 map 的遍历 map 的赋值 ...

  5. 【Go语言】map在goroutine通信中的使用问题

    简介 本篇文章的主要内容是解决go语言map在使用中遇到的两个问题,对于初学者是不可避免的坑 一.cannot assign to struct field 当map中存在struct类型的成员,如果 ...

  6. Go语言的map

    map一般是以库的方式提供,在C++和C#和JAVA中都需要引用相应的库而Go语言不需要引入库,可以直接方便使用 定义:map是一堆键值对的未排序集合.无序 1.声明变量: map的声明基本上没有多余 ...

  7. Go语言的map如何判断key是否存在

    判断方式为value,ok := map[key], ok为true则存在 package main import "fmt" func main() { demo := map[ ...

  8. go语言学习--map中键值得删除

    测试 map1 中是否存在 key1: 在例子 8.1 中,我们已经见过可以使用 val1 = map1[key1] 的方法获取 key1 对应的值 val1.如果 map 中不存在 key1,val ...

  9. go语言学习--map类型的切片

    今天在项目中遇到了一个切片的map,记录下map切片的使用 package main import "fmt" func main() { // Version A: items ...

随机推荐

  1. SQLHelper.cs类 微软C#版

    using System; using System.Data; using System.Xml; using System.Data.SqlClient; using System.Collect ...

  2. 《Dotnet9》建站-本站使用的什么主题?

    时间如流水,只能流去不流回! 点赞再看,养成习惯,这是您给我创作的动力! 本文 Dotnet9 https://dotnet9.com 已收录,站长乐于分享dotnet相关技术,比如Winform.W ...

  3. 学习Python编程技术的流程与步骤,自学与参加培训学习都适用

     一.清楚学习目标 无论是学习什么知识,都要有一个对学习目标的清楚认识.只有这样才能朝着目标持续前进,少走弯路,从学习中得到不断的提升,享受python学习计划的过程. 虽然目前的编程语言有很多,但是 ...

  4. net core 3.1 跨域 Cors 找不到 “Access-Control-Allow-Origin”

    首先在ConfigureServices添加 public void ConfigureServices(IServiceCollection services) { services.AddCors ...

  5. java8新特性,你有用起来了吗?(精编)

      2019年9月19日java13已正式发布,感叹java社区强大,经久不衰.由于国内偏保守,新东西总要放一放,让其他人踩踩坑,等稳定了才会去用.并且企业目的还是赚钱,更不会因为一个新特性去重构代码 ...

  6. Java面试题_第三阶段(Spring、MVC、IOC、AOP、DI、MyBatis、SSM、struts2)

    1.1 何为Spring Bean容器?Spring Bean容器与Spring IOC 容器有什么不同吗? 答:1)用于创建bean对象,管理bean对象的那个容器. 2)Spring IOC 容器 ...

  7. mac-安装sshpass

    在配置了ssh免密认证id_rsa.pub之后,在cmd终端可以实现免密登陆对应配置了密钥的服务器,但是在python程序中,想要调用cmd执行免密操作,还需要安装sshpass,sshpass用于非 ...

  8. Snack3 之 Jsonpath使用

    Snack3 之 Jsonpath使用 一. Snack3 和 JSONPath 介绍 Snack3 是一个支持JSONPath的JSON框架.JSONPath是一个很强大的功能,也可以在Java框架 ...

  9. xms新版发布

    基于.net core 3.0.101 github地址: https://github.com/migomiddle/xms 码云地址: https://gitee.com/migomiddle/x ...

  10. WebShell代码分析溯源(十一)

    WebShell代码分析溯源(十一) 一.一句话变形马样本 <?php $e = $_REQUEST['e'];declare(ticks=1);register_tick_function ( ...