当前位置:网站首页>Map (set) operation in go language
Map (set) operation in go language
2022-06-23 08:35:00 【Unfortunately, no if o】
map:
1. Definition
key-value Formal data storage data structure , And JAVA in map It's the same thing .
2.map Getting started
//map Statement of ( Definition )
var m map[string]int
//map The initialization
m = make(map[string]int)
//map Add data to
m["age"] = 100
// Print map data
fmt.Printf("%v", m)Be careful :
1.map Of Statement and map Of initialization Not the same concept . uninitialized Of map yes nil,nil Of map It is not allowed to add values , Otherwise (panic: assignment to entry in nil map) error , But you can get the default value .
//map Statement of ( Definition ), Not initialized
var m map[string]int
fmt.Printf("%v\n", m["age"]) // Able to print 0
m["age"] = 100 // Report errors panic: assignment to entry in nil map
fmt.Printf("%v\n", m) // Don't execute 2. So-called nil Can be interpreted as Null pointer , It does not point to any memory address , and make() The method is to map Allocate memory address .
3.make() Can be used to initialize map, It's fine too map Declaration time Direct initialization .
// Statement + initialization
m := map[string]int{"age": 10}
fmt.Printf("%v\n", m["age"]) //10
m["sex"] = 0 // Set up sex=0
fmt.Printf("%v\n", m) //map[age:10 sex:0]3.map Add, delete, change and check operation
// Statement map
var m map[string]string
// initialization map
m = make(map[string]string)
//map Add data
m["name"] = "mongobx"
m["age"] = "18"
m["sex"] = "male"
fmt.Printf("%v\n", m) //map[age:18 name:mongobx sex:male]
//map Delete data
delete(m, "sex")
fmt.Printf("%v\n", m) //map[age:18 name:mongobx]
//map Modifying data
m["age"] = "22"
fmt.Printf("%v\n", m) //map[age:22 name:mongobx]
//map Query data ( Three )
// Inquire about 1
name := m["name"]
fmt.Printf("%v\n", name) //mongobx
// Inquire about 2
name, ok := m["name"] //name There is ok by true, Otherwise false
fmt.Printf("%v-%v\n", name, ok) //mongobx-tru
// Inquire about 3
_, ok := m["name"]
fmt.Printf("%v\n", ok) //true
4.map The traversal :
// Statement map
var m map[string]string
// initialization map
m = make(map[string]string)
//map Add data
m["name"] = "mongobx"
m["age"] = "18"
m["sex"] = "male"
fmt.Printf("%v\n", m)
// Traverse key
for key := range m {
fmt.Printf("%v\n", key)
}
// Traverse value
for _, value := range m {
fmt.Printf("%v\n", value)
}
// Traverse key,value
for key, value := range m {
fmt.Printf("%v-%v\n", key, value)
}边栏推荐
- MySQL brochure notes 5 InnoDB record storage structure
- Vulnhub | DC: 3 |【实战】
- Code quality level 3 - readable code
- How can easycvr access the Dahua CVS video recorder and download a video file with an empty name?
- Ad object of Active Directory
- Image segmentation - improved network structure
- How to evaluate code quality
- 1-渐变、阴影和文本
- Object. Defineproperty() and data broker
- Explanation on webrtc's stun/turn service in tsingsee green rhino video
猜你喜欢

Vulnhub | DC: 3 |【实战】

Deep learning ----- different methods to implement lenet-5 model

观察者模式

Ad object of Active Directory

Deep learning ----- different methods to realize vgg16

Does huangrong really exist?

Qualcomm 9x07 two startup modes

PHP serialization and deserialization CTF

实战监听Eureka client的缓存更新

Basic use of check boxes and implementation of select all and invert selection functions
随机推荐
Does huangrong really exist?
Copy image bitmap by C # memory method
你有一串代码,但是不支持低版本Go时;判断Go版本号,您值得拥有!
Multi-scale feature combination in target detection
Map interface and its sub implementation classes
How to start Jupiter notebook in CONDA virtual environment
usb peripheral 驱动 - debug
Deep learning ----- different methods to realize vgg16
Object.defineProperty() 和 数据代理
正则表达式使用案例
Introduction to typescript and basic types of variable definitions
Talk about the implementation principle of @autowired
MySQL小册子笔记 5 InnoDB 记录存储结构
aquatone工具 中的2个bug修复
为什么用生长型神经气体网络(GNG)?
[paper notes] catching both gray and black swans: open set supervised analog detection*
爬虫框架
Idea true permanent activation method and permanent activation code tutorial
数据资产为王,解析企业数字化转型与数据资产管理的关系
4-绘制椭圆、使用定时器