当前位置:网站首页>go语言映射操作
go语言映射操作
2022-04-23 06:15:00 【玩哈哈527】
package main
import "fmt"
//创建和使用映射
func main() {
// 创建一个映射,键的类型是 string,值的类型是 int
dict:=make(map[string]int)
fmt.Println(dict)
// 创建一个映射,键和值的类型都是 string
// 使用两个键值对初始化映射
dict1:=map[string]string {
"apple":"good","orange":"bad"}
fmt.Println(dict1)
// 创建一个映射,不能使用字符串切片作为映射的键,但可以用切片作为值
//dict2:=map[string[]]int{}//编译错误
dict2:=map[string][]string{
}
dict3:=map[int][]string{
}
fmt.Println(dict2)
fmt.Println(dict3)
//使用 range 迭代映射,返回键值对
//创建一个映射
dict4:=map[string]int{
"a":1,"b":2,"c":3,"d":4}
fmt.Println(dict4)
for key,value:=range dict4{
fmt.Printf("key:%s value:%d\n",key,value)
}
//判断是否存在键值对
var str string="s"
value,exists:=dict4[str]
if exists {
fmt.Printf("存在键%s的匹配值,其值为%d\n",str,value)
}else
{
fmt.Printf("不存在键%s的匹配值\n",str)
}
delete(dict4,"a")//删除一个键值对
fmt.Println(dict4)
//在函数间传递映射
ss:=map[string]int{
"s1":1,"s2":2,"s3":3,"s4":4}
fmt.Println(ss)
ss1:=Remove(ss)
fmt.Println(ss1)
}
func Remove(ss map[string]int) map[string]int {
delete(ss,"s1")
return ss
}
/* 输出结果 map[] map[apple:good orange:bad] map[] map[] map[a:1 b:2 c:3 d:4] key:a value:1 key:b value:2 key:c value:3 key:d value:4 不存在键s的匹配值 map[b:2 c:3 d:4] map[s1:1 s2:2 s3:3 s4:4] map[s2:2 s3:3 s4:4] */
版权声明
本文为[玩哈哈527]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_28058509/article/details/119213017
边栏推荐
- STM32多路测温无线传输报警系统设计(工业定时测温/机舱温度定时检测等)
- AUTOSAR从入门到精通100讲(八十四)-UDS之时间参数总结篇
- Résolution du système
- PyTorch 19. Differences and relations of similar operations in pytorch
- AUTOSAR从入门到精通100讲(八十六)-UDS服务基础篇之2F
- AUTOSAR从入门到精通100讲(八十三)-BootLoader自我刷新
- 美摄助力百度“度咔剪辑”,让知识创作更容易
- 【期刊会议系列】IEEE系列模板下载指南
- imx6ull-qemu 裸机教程2:USDHC SD卡
- Cmder Chinese garbled code problem
猜你喜欢

Chapter 8 generative deep learning

机器视觉系列(02)---TensorFlow2.3 + win10 + GPU安装

Systrace parsing

MySQL installation and configuration - detailed tutorial

Modifying a column with the 'identity' pattern is not supported

SSL / TLS application example

电力行业巡检对讲通信系统

Detailed explanation of device tree

RISCV MMU 概述
![[point cloud series] pnp-3d: a plug and play for 3D point clouds](/img/83/3662bc668602110236e43ee0b9d7ac.png)
[point cloud series] pnp-3d: a plug and play for 3D point clouds
随机推荐
scons 搭建嵌入式arm编译
PyTorch 9. optimizer
PyTorch 19. PyTorch中相似操作的区别与联系
【点云系列】Neural Opacity Point Cloud(NOPC)
PyTorch 22. PyTorch常用代码段合集
PyTorch 21. PyTorch中nn.Embedding模块
ECDSA 签名验证原理及C语言实现
机器学习——PCA与LDA
RISCV MMU 概述
“泉”力以赴·同“州”共济|北峰人一直在行动
Systrace 解析
x509解析
imx6ull-qemu 裸机教程1:GPIO,IOMUX,I2C
Thanos. SH kill bully script, easily delete half of the files in the system at random
美摄科技起诉天目传媒使用火山引擎侵权代码的声明
多机多卡训练时的错误
pth 转 onnx 时出现的 gather、unsqueeze 等算子
PyTorch 14. Module class
【点云系列】Multi-view Neural Human Rendering (NHR)
Modifying a column with the 'identity' pattern is not supported