当前位置:网站首页>Section 2 map and structure in Chapter 6
Section 2 map and structure in Chapter 6
2022-04-23 03:27:00 【Python code doctor】
Map
summary
Go In language map( mapping 、 Dictionaries ) It's a built-in data structure , It's a disorder Of key—value For the collection of , For example, ID card number is used as a unique key to identify a person's information .
info := map[int]string{
110: "mark",
111: "yoyo",
112: "lili",
}
:
map The format is :
map[KeyType]valueType
In a map All the keys in the are only Of , And it has to be support == and != The type of operator , section 、 Functions and structure types that contain slices because of their referential semantics , Can't be a key to a map , Using these types can cause compilation errors :
dict := map[ []string ]int{} //err, invalid map key type []string
map Values can be any type , There is no limit to .map All keys in must have the same data type , Value must also be how , But the data types of keys and values can be different .
Be careful :map yes disorder Of , We can't decide the return order , therefore , The order of printing results may be different each time .
Create and initialize
map The creation of
var m1 map[int]string // Just a statement map, Not initialized , This is empty (nil)map
fmt.Println(m1 == nil) //true
//m1[1] = "mike" //err, panic: assignment to entry in nil map
// m2, m3 The creation method of is equivalent
m2 := map[int]string{
}
m3 := mmake(map[int]string)
fmt.Printlnn(m2, m3) //map[] map[]
m4 := make(mmap[int]string, 10) // The second parameter specifies the capacity
fmt.Println(m4) //map[]
initialization
//1、 Definitions are initialized at the same time
var m1 map[int]string = map[int]string{
1: "mike", 2: "yoyo"}
fmt.Println(m1) //map[1:mike 2:yoyo]
//2、 Automatic derivation type :=
m2 := map[int]string{
1: "mike", 2: "yoyo"}
fmt.Println(m2)
Common operations
assignment
m1 := map[int]string{
1: "mike", 2: "yoyo"}
m1[1] = "xxx" // modify
m1[3] = "lily" // Additional ,go The bottom layer will automatically be map Allocate space
fmt.Println(m1) //map[1:xxx 2:yoyo 3: lily]
m2 := make(map[int]string, 10) // establish map
m2[0] = "aaa"
m2[1] = "bbb"
fmt.Println(m2) //map[0:aaa 1:bbb]
fmt.Println(m2[0], m2[1]) //aaa bbb
Traverse
m1 := map[int]string{
1: "mike", 2: "yoyo"}
// Iterate through 1, The first return value is key, The second return value is value
for k,v := range m1 {
fmt.Printf("%d ----> %s\n", k, v)
//1 ----> mike
//2 ----> yoyo
}
// Iterate through 2, The first return value is key, The second return value is value( Omission )
for k := range m1 {
fmt.Printf("%d ----> %s\n", k, m1[k])
//1 ----> mike
//2 ----> yoyo
}
// Judge a certain key The corresponding value Whether there is , The first return value is value( If it exists )
value, ok := m1[1]
fmt.Println("value = ", value, ",ok = ", ok) //value = mike, ok = true
value2, ok2 := m1[3]
fmt.Println("value2 = ",value2, ", ok2 = ", ok2) //value2 = , ok2 = false
Delete
m1 := map[int]string{
1: "mike", 2: "yoyo", 3: "lily"}
// Iterate through 1, The first return value is key, The second return value is value
for k,v := range m1 {
fmt.Printf("%d ----> %s\n", k, v)
//1 ----> mike
//2 ----> yoyo
//3 ----> lily
}
delete(m1, 2) // Delete key The value is 3 Of map
for k, v := range m1 {
fmt.Printf("%d ----> %s\n", k, v)
//1 ----> mike
//3 ----> lily
}
map Do function parameters
Passing a map between functions does not make a copy of that map , Not value passing , It's a reference to pass :
func DeleteMap(m map[int]string,key int) {
delete(m, key) // Delete key The value is 3 Of map
for k, v := range m {
fmt.Printf("len(m)=%d, %d ----> %s\n", len(m), k, v)
//len(m)=2, 1 ----> mike
//len(m)=2, 3 ----> lily
}
}
func main() {
m := map[int]strinng{
1: "mike", 2: "yoyo", 3: "lily"}
DeleteMap(m,2) // Delete key The value is 3 Of map
for k, v := range m {
fmt.Printf(""len(m)=%d, %d ----> %s\n", len(m), k, v)
//len(m)=2, 1 ----> mike
//len(m)=2, 3 ----> lily
}
}
Structure
Type of structure
Sometimes we need to combine different types of data into an organic whole , Such as : A student has a student number / full name / Gender / Age / Address and other properties . Obviously, it is tedious to define the above variables alone , Data is not easy to manage .

A structure is an aggregate data type , It is a data set composed of a series of data of the same type or different types . Each data is called a member of the structure .
Structure initialization
//1、 Print members
var s1 Student = Student{
1, "mark", 'm', 18, "sz"}
// result :id = 1, name = mark, sex = m, age =18, addr =sz
//2、 Member variable assignment
var s2 Student
s2.id = 2
s2.name = "yoyo"
s2.sex = 'f'
s2.age = 16
s2.addr = "guangzhou"
fmt.Println(s2) //{2 yoyo 102 16 guangzhou}
Structure comparison
If all members of a structure are comparable , So the structure can also be compared , In that case, two structures can be used == or != Operator to compare , But does not support > or < .
func := Student{
s1 := Student{
1, "mike", 'm', 18, "sz"}
s2 := Student{
1, "mike", 'm', 18, "sz"}
fmt.Println(fmt.Println("s1 == s2", s1 == s2) //s1 == s2 true
fmt.Println("s1 != s2", s1 != s2) //s1 != s2 false
}
Array of structs
type Student struct {
id int
name string
score int
}
func main() {
// Array of structs
var arr []Student = []Student{
Student{
1, " Li Bai ", 100},
Student{
2, " Wang wei ", 100},
Student{
3, " Du Fu ", 100},}
fmt.Println(arr)
// Print structure information circularly
for i := 0; i < len(arr); i++ {
fmt.Println(arr[i])
}
}
Structure as function parameter
type Hero struct {
name string
age int
power int
}
// Structure as function parameter
func test18(h Hero) {
h.power = 120
fmt.Println(h)
}
func main() {
// Structural variable
h := Hero{
" Iron man ", 30, 100}
// Value passed
test18(h)
fmt.Println(h)
}
版权声明
本文为[Python code doctor]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220619244268.html
边栏推荐
- 2022 group programming ladder game simulation L2-4 Zhezhi game (25 points)
- 2022 团体程序设计天梯赛 模拟赛 1-8 均是素数 (20 分)
- IDEA查看历史记录【文件历史和项目历史】
- 场景题:A系统如何使用B系统的页面
- L3-011 direct attack Huanglong (30 points)
- . NETCORE sets the API post mode, which can accept parameters directly in parentheses
- Data mining series (3)_ Data mining plug-in for Excel_ Estimation analysis
- Comprehensive calculation of employee information
- Web Course Design - his system
- Visual programming - drawing assignment
猜你喜欢
![General test technology [II] test method](/img/b7/f661f446616ad6bfbbf48eb03ea82d.png)
General test technology [II] test method

Huawei mobile ADB devices connection device is empty

“如何实现集中管理、灵活高效的CI/CD”在线研讨会精彩内容分享

C interface
![Detailed description of MySQL index [B + tree index, hash index, full-text index, overlay index]](/img/1a/a22b4a35d3c083438d0f766a5ecb08.png)
Detailed description of MySQL index [B + tree index, hash index, full-text index, overlay index]

Log4net is in Net core usage

Quartz. Www. 18fu Used in net core

L3-011 直捣黄龙 (30 分)

2022 团体程序设计天梯赛 模拟赛 L2-4 哲哲打游戏 (25 分)

Test questions (2)
随机推荐
超好用的【通用Excel导入功能】
场景题:A系统如何使用B系统的页面
超好用的Excel异步导出功能
Initial experience of talent plan learning camp: communication + adhering to the only way to learn open source collaborative courses
MySQL索引详解【B+Tree索引、哈希索引、全文索引、覆盖索引】
2021-08-31
幂等性实践操作,基于业务讲解幂等性
Visual programming - Experiment 1
Chapter 9 of C language programming (fifth edition of Tan Haoqiang) analysis and answer of exercises for users to establish their own data types
The query type of MySQL is very inefficient.
2022 团体程序设计天梯赛 模拟赛 L2-1 盲盒包装流水线 (25 分)
. NETCORE sets the API post mode, which can accept parameters directly in parentheses
Query stored procedures in PostgreSQL
JS implementation of new
AWS from entry to actual combat: creating accounts
Configure automatic implementation of curd projects
QT learning summary
Chapter 8 of C language programming (fifth edition of Tan Haoqiang) is good at using pointer exercises to analyze and answer
MySQL query specifies that a row is sorted to the first row
月薪10k-20k都无法回答的事务问题,你会吗?