当前位置:网站首页>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
边栏推荐
猜你喜欢

x509解析

Unwind 栈回溯详解

【无标题】制作一个0-99的计数器,P1.7接按键,P2接数码管段,共阳极数码管,P3.0,P3.1接数码管位码,每按一次键,数码管显示加一。请写出单片机的C51代码

【点云系列】Relationship-based Point Cloud Completion

Chapter 4 pytoch data processing toolbox

南方投资大厦SDC智能通信巡更管理系统

“泉”力以赴·同“州”共济|北峰人一直在行动

MySQL installation and configuration - detailed tutorial

使用proteus仿真STM32超声波SRF04测距!Code+Proteus

EasyUI combobox determines whether the input item exists in the drop-down list
随机推荐
吴恩达编程作业——Logistic Regression with a Neural Network mindset
基于Labview上位机的51单片机步进电机控制系统(上位机代码+下位机源码+ad原理图+51完整开发环境)
机器视觉系列(02)---TensorFlow2.3 + win10 + GPU安装
主流 RTOS 评估
EMMC/SD学习小记
城市应急管理|城市突发事故应急通信指挥调度系统
Chapter 3 pytoch neural network toolbox
rearrange 和 einsum 真的优雅吗
广西电网|应急空天一体化通信系统方案
Paddleocr image text extraction
The simplest and complete example of libwebsockets
安装 pycuda 出现 PEP517 的错误
PyTorch 18. torch.backends.cudnn
Résolution du système
[3D shape reconstruction series] implicit functions in feature space for 3D shape reconstruction and completion
被 onnx.checker.check_model 检查出的常见错误
Int8 quantification and inference of onnx model using TRT
地铁无线对讲系统
unhandled system error, NCCL version 2.7.8
PyTorch 17. GPU concurrency