当前位置:网站首页>golang 入门--定义map的6种方式
golang 入门--定义map的6种方式
2022-04-22 21:27:00 【猎人在吃肉】
文章目录
方式1:
var heroes map[string]string = map[string]string{
"heroe1": "卢俊义",
"heroe2": "林冲",
}
heroes["heroe3"] = "阮小七"
fmt.Println("方式2: ", heroes)
方式1-2:类型推导同时赋值(推荐)
// 方式2-2 (类型推导)
heroes2 := map[string]string{
"heroe1": "卢俊义",
"heroe2": "林冲",
}
heroes2["heroe3"] = "阮小七"
fmt.Println("方式2-2: ", heroes2)
方式1-3:先类型推导,后赋值(推荐)
heroes3 := map[string]string{
}
heroes3["heroe1"] = "武松"
fmt.Println("方式1-3: ", heroes3)
方式2:make先定义,再初始化,接着再赋值
var cities3 map[string]string // 只是定义,没有分配空间,无法调用
// a["no1"] = "宋江" //此时,没有分配空间,赋值会报错
cities3 = make(map[string]string) // map分配数据空间
cities3["no1"] = "黑龙江"
cities3["no2"] = "吉林"
cities3["no3"] = "辽宁"
fmt.Println("方式2: ", cities3)
方式2-2: make,定义并初始化
var cities map[string]string = make(map[string]string) // 定义,分配数据空间,赋值
cities["no1"] = "黑龙江"
cities["no2"] = "吉林"
cities["no3"] = "辽宁"
fmt.Println("方式2-2: ", cities)
方式2-3:make,类型推导(推荐)
cities2 := make(map[string]string)
cities2["no1"] = "黑龙江"
cities2["no2"] = "吉林"
cities2["no3"] = "辽宁"
fmt.Println("方式2-3: ", cities2)
版权声明
本文为[猎人在吃肉]所创,转载请带上原文链接,感谢
https://xiaojin21cen.blog.csdn.net/article/details/124336803
边栏推荐
- Study record of the 8th week of graduation project
- 2022-4-22 Leetcode 279. Complete square
- Dernière version du filigrane + code source de l'applet CPS à emporter
- Navicat for MySQL 连接 Docker中运行的Mysql出现1251- Client does not support authentication protocol 错误
- What is the current situation of domestic computer hardware?
- Openvx's immediate mode and graph mode and examples
- BLE---Advertisement data format & service
- uClibc 和 Glibc 不一样
- MySQL is still suitable for Silicon Valley courses (I)
- Big talk test data (I)
猜你喜欢

最新版去水印+外賣cps小程序源碼

Soochow securities x kangaroo cloud: the data is easily available and has millisecond response ability. What did Soochow securities do right?

keras逻辑回归进行贷款风险预测

Spark-2.4.2编译安装

Freemodbus Quick Start Guide

Big talk test data (I)

MySQL is still suitable for Silicon Valley courses (I)

All paths of 344 leetcode binary tree

1251 - client does not support authentication protocol error in MySQL running in Navicat for MySQL connection docker

Huawei interview question: "how can an 800kg cow cross a 700kg bridge?" You can live your life as you think
随机推荐
After five years of graduation, how to increase the monthly salary from 5K to 50W +, what core skills do you need to master?
MySQL specifies field sorting and user-defined sorting location
MySql指定字段排序、自定义排序位置
uClibc 和 Glibc 不一样
Time report filling rules
[tool configuration] generated by designer in visual studio Solution to the problem of UI file not being updated in time
Leetcode-209-subarray with the smallest length
How to run PHP files with Largon framework
[bat] view the file MD5
QT qstring stepping pit
Solving chrome cross domain problems - cross domain series
驱动开发总结记录
新闻速递 I MobTech通过中国信通院“安全专项评测”
Force buckle -931 Minimum sum of descent path
Leetcode-238 - product of arrays other than itself
Basic principle and preliminary practice of building local server and XSS by wampserver (I)
MySQL尚硅谷课程查缺补漏适合基础(一)
基于JS怎么编写看字说颜色小游戏
leetcode-470. Rand10() is implemented with rand7()
Openvx - read and write image file [pgm format] as VX_ Image objects, and write operations