Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
mannadt
code_reading
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
code_reading
/
basic
/
data
/
map.go
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
59 lines (51 loc) · 1.56 KB
master
Breadcrumbs
code_reading
/
basic
/
data
/
map.go
Copy path
Top
File metadata and controls
Code
Blame
59 lines (51 loc) · 1.56 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package main
import "fmt"
func mapper() {
// var mp map[string]int 不会初始化
mp := make(map[string]int)
mp["Tom"] = 10
age, ok := mp["Tom"]
fmt.Println(age, ok)
age1 := mp["Tom"]
age2 := mp["Tom2"]
fmt.Println(age1, age2)
for key, value := range mp {
fmt.Println(key, value)
}
for key := range mp {
fmt.Println(key, mp[key])
}
var data = []int{1, 3, 5}
for key := range data {
fmt.Println(key, data[key])
}
}
// go tool compile -S map.go
func mapCompile() {
m := make(map[string]int, 9)
key := "test"
m[key] = 1
_, ok := m[key]
if ok {
delete(m, key)
}
}
/*
源码文件:runtime/map.go
初始化:makemap
1. map中bucket的初始化 makeBucketArray
2. overflow 的定义为哈希冲突的值,用链表法解决
赋值:mapassign
1. 不支持并发操作 h.flags&hashWriting
2. key的alg算法包括两种,一个是equal,用于对比;另一个是hash,也就是哈希函数
3. 位操作 h.flags ^= hashWriting 与 h.flags &^= hashWriting
4. 根据hash找到bucket,遍历其链表下的8个bucket,对比hashtop值;如果key不在map中,判断是否需要扩容
扩容:hashGrow
1. 扩容时,会将原来的 buckets 搬运到 oldbuckets
读取:mapaccess
1. mapaccess1_fat 与 mapaccess2_fat 分别对应1个与2个返回值
2. hash 分为低位与高位两部分,先通过低位快速找到bucket,再通过高位进一步查找,对后对比具体的key
3. 访问到oldbuckets中的数据时,会迁移到buckets
删除:mapdelete
1. 引入了emptyOne与emptyRest,后者是为了加速查找
*/
You can’t perform that action at this time.