go语言之map
go语言的map就相当于python的dict
1、map的初始化
- //创建map
- //k的类型是int,v的类型是string
- var test25_1 map[int]string
- fmt.Println(test25_1)
- //map[]
- fmt.Println(test25_1 == nil)
- //true
- test25_2 := map[int]string{}
- test25_3 := make(map[int]string)
- fmt.Println(test25_2)
- //map[]
- fmt.Println(test25_3)
- //map[]
- //初始化一个有容量的map
- test25_4 := make(map[int]string,10)
- fmt.Println(test25_4)
- //map[]
- //初始化map
2、map的初始化
- //定义的同时初始化
- var test25_5 map[int] string = map[int]string{1:"test1",2:"test2"}
- fmt.Println(test25_5)
- //map[1:test1 2:test2]
- //自动推倒类型
- var test25_6 = map[int]string{3:"test3",4:"test4"}
- fmt.Println(test25_6)
- //map[3:test3 4:test4]
3、map的键值操作,增和改
- //键值操作
- //修改
- test25_6[3] = "test333"
- fmt.Println(test25_6)
- //map[3:test333 4:test4]
- //增加
- test25_6[5] = "test5"
- fmt.Println(test25_6)
- //map[3:test333 4:test4 5:test5]
4、map的遍历操作
- //map的遍历操作
- for k,v := range test25_6{
- fmt.Printf("k值为%d,v值为%s\n",k,v)
- }
- //k值为5,v值为test5
- //k值为3,v值为test333
- //k值为4,v值为test
- for k:= range test25_6{
- fmt.Printf("k值为%d,v值为%s\n",k,test25_6[k])
- }
- //k值为3,v值为test333
- //k值为4,v值为test4
- //k值为5,v值为test5
5、判断map是否存在某个k
- //判断某个k对应的v是否存在
- //value为k对应的值,ok为是否存在
- value,ok := test25_6[3]
- fmt.Println(value,ok)
- //test333 true
- value1,ok1 := test25_6[7]
- fmt.Println(value1,ok1)
- //false
6、删除map中的某个k
- fmt.Println(test25_6)
- //map[3:test333 4:test4 5:test5]
- delete(test25_6,3)
- fmt.Println(test25_6)
- //map[4:test4 5:test5]
Go 语言Map(集合)
Map 是一种无序的键值对的集合。Map 最重要的一点是通过 key 来快速检索数据,key 类似于索引,指向数据的值。
Map 是一种集合,所以我们可以像迭代数组和切片那样迭代它。不过,Map 是无序的,我们无法决定它的返回顺序,这是因为 Map 是使用 hash 表来实现的。
定义 Map
可以使用内建函数 make 也可以使用 map 关键字来定义 Map:
- /* 声明变量,默认 map 是 nil */
- var map_variable map[key_data_type]value_data_type
- /* 使用 make 函数 */
- map_variable := make(map[key_data_type]value_data_type)
如果不初始化 map,那么就会创建一个 nil map。nil map 不能用来存放键值对
实例
下面实例演示了创建和使用map:
- package main
- import "fmt"
- func main() {
- var countryCapitalMap map[string]string /*创建集合 */
- countryCapitalMap = make(map[string]string)
- /* map插入key - value对,各个国家对应的首都 */
- countryCapitalMap [ "France" ] = "巴黎"
- countryCapitalMap [ "Italy" ] = "罗马"
- countryCapitalMap [ "Japan" ] = "东京"
- countryCapitalMap [ "India " ] = "新德里"
- /*使用键输出地图值 */
- for country := range countryCapitalMap {
- fmt.Println(country, "首都是", countryCapitalMap [country])
- }
- /*查看元素在集合中是否存在 */
- capital, ok := countryCapitalMap [ "American" ] /*如果确定是真实的,则存在,否则不存在 */
- /*fmt.Println(capital) */
- /*fmt.Println(ok) */
- if (ok) {
- fmt.Println("American 的首都是", capital)
- } else {
- fmt.Println("American 的首都不存在")
- }
- }
以上实例运行结果为:
- France 首都是 巴黎
- Italy 首都是 罗马
- Japan 首都是 东京
- India 首都是 新德里
- American 的首都不存在
delete() 函数
delete() 函数用于删除集合的元素, 参数为 map 和其对应的 key。实例如下:
- package main
- import "fmt"
- func main() {
- /* 创建map */
- countryCapitalMap := map[string]string{"France": "Paris", "Italy": "Rome", "Japan": "Tokyo", "India": "New delhi"}
- fmt.Println("原始地图")
- /* 打印地图 */
- for country := range countryCapitalMap {
- fmt.Println(country, "首都是", countryCapitalMap [ country ])
- }
- /*删除元素*/ delete(countryCapitalMap, "France")
- fmt.Println("法国条目被删除")
- fmt.Println("删除元素后地图")
- /*打印地图*/
- for country := range countryCapitalMap {
- fmt.Println(country, "首都是", countryCapitalMap [ country ])
- }
- }
以上实例运行结果为:
- 原始地图
- India 首都是 New delhi
- France 首都是 Paris
- Italy 首都是 Rome
- Japan 首都是 Tokyo
- 法国条目被删除
- 删除元素后地图
- Italy 首都是 Rome
- Japan 首都是 Tokyo
- India 首都是 New delhi
基于 go 实现简单 HashMap,暂未做 key 值的校验。
- package main
- import (
- "fmt"
- )
- type HashMap struct {
- key string
- value string
- hashCode int
- next *HashMap
- }
- var table [16](*HashMap)
- func initTable() {
- for i := range table{
- table[i] = &HashMap{"","",i,nil}
- }
- }
- func getInstance() [16](*HashMap){
- if(table[0] == nil){
- initTable()
- }
- return table
- }
- func genHashCode(k string) int{
- if len(k) == 0{
- return 0
- }
- var hashCode int = 0
- var lastIndex int = len(k) - 1
- for i := range k {
- if i == lastIndex {
- hashCode += int(k[i])
- break
- }
- hashCode += (hashCode + int(k[i]))*31
- }
- return hashCode
- }
- func indexTable(hashCode int) int{
- return hashCode%16
- }
- func indexNode(hashCode int) int {
- return hashCode>>4
- }
- func put(k string, v string) string {
- var hashCode = genHashCode(k)
- var thisNode = HashMap{k,v,hashCode,nil}
- var tableIndex = indexTable(hashCode)
- var nodeIndex = indexNode(hashCode)
- var headPtr [16](*HashMap) = getInstance()
- var headNode = headPtr[tableIndex]
- if (*headNode).key == "" {
- *headNode = thisNode
- return ""
- }
- var lastNode *HashMap = headNode
- var nextNode *HashMap = (*headNode).next
- for nextNode != nil && (indexNode((*nextNode).hashCode) < nodeIndex){
- lastNode = nextNode
- nextNode = (*nextNode).next
- }
- if (*lastNode).hashCode == thisNode.hashCode {
- var oldValue string = lastNode.value
- lastNode.value = thisNode.value
- return oldValue
- }
- if lastNode.hashCode < thisNode.hashCode {
- lastNode.next = &thisNode
- }
- if nextNode != nil {
- thisNode.next = nextNode
- }
- return ""
- }
- func get(k string) string {
- var hashCode = genHashCode(k)
- var tableIndex = indexTable(hashCode)
- var headPtr [16](*HashMap) = getInstance()
- var node *HashMap = headPtr[tableIndex]
- if (*node).key == k{
- return (*node).value
- }
- for (*node).next != nil {
- if k == (*node).key {
- return (*node).value
- }
- node = (*node).next
- }
- return ""
- }
- //examples
- func main() {
- getInstance()
- put("a","a_put")
- put("b","b_put")
- fmt.Println(get("a"))
- fmt.Println(get("b"))
- put("p","p_put")
- fmt.Println(get("p"))
- }
go语言之map的更多相关文章
- go语言学习--map的并发
go提供了一种叫map的数据结构,可以翻译成映射,对应于其他语言的字典.哈希表.借助map,可以定义一个键和值,然后可以从map中获取.设置和删除这个值,尤其适合数据查找的场景.但是map的使用有一定 ...
- GO_05:GO语言基础map与函数
1. map 1. 类似其它语言中的哈希表活着字典,以 key-value 形式存储数据 2. key 必须是支持 == 或 != 比较运算的类型,不可以是函数.map 或 slice 3. map ...
- GO语言基础map与函数
1. map 1. 类似其它语言中的哈希表活着字典,以 key-value 形式存储数据 2. key 必须是支持 == 或 != 比较运算的类型,不可以是函数.map 或 slice 3. map ...
- 深度解密Go语言之 map
目录 什么是 map 为什么要用 map map 的底层如何实现 map 内存模型 创建 map 哈希函数 key 定位过程 map 的两种 get 操作 如何进行扩容 map 的遍历 map 的赋值 ...
- 【Go语言】map在goroutine通信中的使用问题
简介 本篇文章的主要内容是解决go语言map在使用中遇到的两个问题,对于初学者是不可避免的坑 一.cannot assign to struct field 当map中存在struct类型的成员,如果 ...
- Go语言的map
map一般是以库的方式提供,在C++和C#和JAVA中都需要引用相应的库而Go语言不需要引入库,可以直接方便使用 定义:map是一堆键值对的未排序集合.无序 1.声明变量: map的声明基本上没有多余 ...
- Go语言的map如何判断key是否存在
判断方式为value,ok := map[key], ok为true则存在 package main import "fmt" func main() { demo := map[ ...
- go语言学习--map中键值得删除
测试 map1 中是否存在 key1: 在例子 8.1 中,我们已经见过可以使用 val1 = map1[key1] 的方法获取 key1 对应的值 val1.如果 map 中不存在 key1,val ...
- go语言学习--map类型的切片
今天在项目中遇到了一个切片的map,记录下map切片的使用 package main import "fmt" func main() { // Version A: items ...
随机推荐
- SQLHelper.cs类 微软C#版
using System; using System.Data; using System.Xml; using System.Data.SqlClient; using System.Collect ...
- 《Dotnet9》建站-本站使用的什么主题?
时间如流水,只能流去不流回! 点赞再看,养成习惯,这是您给我创作的动力! 本文 Dotnet9 https://dotnet9.com 已收录,站长乐于分享dotnet相关技术,比如Winform.W ...
- 学习Python编程技术的流程与步骤,自学与参加培训学习都适用
一.清楚学习目标 无论是学习什么知识,都要有一个对学习目标的清楚认识.只有这样才能朝着目标持续前进,少走弯路,从学习中得到不断的提升,享受python学习计划的过程. 虽然目前的编程语言有很多,但是 ...
- net core 3.1 跨域 Cors 找不到 “Access-Control-Allow-Origin”
首先在ConfigureServices添加 public void ConfigureServices(IServiceCollection services) { services.AddCors ...
- java8新特性,你有用起来了吗?(精编)
2019年9月19日java13已正式发布,感叹java社区强大,经久不衰.由于国内偏保守,新东西总要放一放,让其他人踩踩坑,等稳定了才会去用.并且企业目的还是赚钱,更不会因为一个新特性去重构代码 ...
- Java面试题_第三阶段(Spring、MVC、IOC、AOP、DI、MyBatis、SSM、struts2)
1.1 何为Spring Bean容器?Spring Bean容器与Spring IOC 容器有什么不同吗? 答:1)用于创建bean对象,管理bean对象的那个容器. 2)Spring IOC 容器 ...
- mac-安装sshpass
在配置了ssh免密认证id_rsa.pub之后,在cmd终端可以实现免密登陆对应配置了密钥的服务器,但是在python程序中,想要调用cmd执行免密操作,还需要安装sshpass,sshpass用于非 ...
- Snack3 之 Jsonpath使用
Snack3 之 Jsonpath使用 一. Snack3 和 JSONPath 介绍 Snack3 是一个支持JSONPath的JSON框架.JSONPath是一个很强大的功能,也可以在Java框架 ...
- xms新版发布
基于.net core 3.0.101 github地址: https://github.com/migomiddle/xms 码云地址: https://gitee.com/migomiddle/x ...
- WebShell代码分析溯源(十一)
WebShell代码分析溯源(十一) 一.一句话变形马样本 <?php $e = $_REQUEST['e'];declare(ticks=1);register_tick_function ( ...