当前位置: 首页 > news >正文

go语言(八)---- map

  1. map的声明方式有以下三种。
package mainimport "fmt"func main() {//第一种声明方式//声明map1是一个map类型,key是String,value是Stringvar myMap1 map[string] stringif myMap1 == nil {fmt.Println("myMap1 是一个空map")}//在使用map前,需要先用make给map分配数据空间myMap1 = make(map[string]string,10)myMap1["one"] = "java"myMap1["two"] = "c"myMap1["three"] = "python"fmt.Println("第一种方式", myMap1)//第二种声明方式myMap2 := make(map[int]string)myMap2[1] = "navy"myMap2[2] = "hj"myMap2[3] = "liu"fmt.Println("第二种方式", myMap2)//第三种声明方式myMap3 := map[string] string{"one": "php","two": "c++","three": "python",}fmt.Println("第三种方式", myMap3)
}

在这里插入图片描述

  1. map的使用方式

map的增删改查

package mainimport "fmt"func main() {cityMap :=make(map[string]string)//添加cityMap["China"] = "Beijing"cityMap["Japan"] = "Tokyo"cityMap["USA"] = "NewYork"//遍历for key,value := range cityMap  {fmt.Println("key = ",key)fmt.Println("value = ",value)}//删除delete(cityMap,"Japan")//修改cityMap["USA"] = "DC"fmt.Println("-------------")//遍历for key, value := range cityMap {fmt.Println("key = ",key)fmt.Println("value = ",value)}
}

在这里插入图片描述

  1. map的传参
package mainimport ("fmt"
)func printMap(cityMap map[string]string) {//cityMap 是一个引用传递for key, value := range cityMap{fmt.Println("key = ",key)fmt.Println("value = ",value)}
}func changeValue(cityMap map[string] string)  {cityMap["England"] = "Lodndon"
}func main() {cityMap := make(map[string]string)//添加cityMap["China"] = "Beijing"cityMap["Japan"] = "Tokyo"cityMap["USA"] = "NewYork"//遍历printMap(cityMap)//增加一个mapchangeValue(cityMap)fmt.Println("---------------")//遍历printMap(cityMap)}

在这里插入图片描述

http://www.lryc.cn/news/285974.html

相关文章:

  • Flutter:跨平台移动应用开发的未来
  • 二维码地址门牌管理系统:智慧城市新篇章
  • 学习JavaEE的日子 day14 继承,super(),this(),重写
  • 一文梳理Windows自启动位置
  • 【Java 设计模式】行为型之策略模式
  • go实现判断20000数据范围内哪些是素数(只能被1和它本身整除的数),采用多协程和管道实现
  • GPT只是开始,Autonomous Agents即将到来
  • ubuntu source: not found
  • Rancher部署k8s集群测试安装nginx(节点重新初始化方法,亲测)
  • SpringBoot结合thymeleaf的HTML页面不能跳转问题踩坑
  • Apache Zeppelin结合Apache Airflow使用1
  • 分组循环A
  • 《WebKit 技术内幕》学习之九(4): JavaScript引擎
  • [SpringBoot2.6.13]FastJsonHttpMessageConverter不生效
  • (delphi11最新学习资料) Object Pascal 学习笔记---第3章第一节(简单语句与复合语句)
  • Unity - 简单音频
  • SpringCloud中服务间通信(应用间通信)-亲测有效-源码下载-连载2
  • Axios取消请求:AbortController
  • 【江科大】STM32:(超级详细)定时器输出比较
  • Go 复合数据类型
  • Redis(01)——常用指令
  • 基本语法和 package 与 jar
  • 本地读取Excel文件并进行数据压缩传递到服务器
  • 【开源】基于JAVA的停车场收费系统
  • 基于java+Springboot操作系统教学交流平台详细设计实现
  • Nginx 基础使用
  • JavaEE:多线程(2):线程状态,线程安全
  • Flutter 自定义AppBar实现滚动渐变
  • 编程语言MoonBit新增矩阵函数的语法糖
  • Angular:跨域请求携带 cookie