網(wǎng)站首頁 編程語言 正文
Arrays (數(shù)組), Slices (切片) 和 Maps (映射) 是常見的一類數(shù)據(jù)結(jié)構(gòu)
1.Arrays (數(shù)組)
- 數(shù)組是定長的。
- 長度不可改變。
初始化
package main import ( "fmt" ) func main() { var scores [10]int scores[0] = 99 fmt.Printf("scoers:%d\n", scores) socres_english := [4]int{99, 100, 100, 99} for index, value := range socres_english { fmt.Printf("%d\t %d", index, value) } }
Starting: C:\Users\livingbody\go\bin\dlv.exe dap --check-go-version=false --listen=127.0.0.1:14487 from c:\Users\livingbody\goworkspace\hello
DAP server listening at: 127.0.0.1:14487
Type 'dlv help' for list of commands.
scoers:[99 0 0 0 0 0 0 0 0 0]
0?? ? 991?? ? 1002?? ? 1003?? ? 99
Process 14356 has exited with status 0
Detaching
dlv dap (15128) exited with code: 0
2.切片
切片是輕量的包含并表示數(shù)組的一部分的結(jié)構(gòu)。
2.1 make創(chuàng)建切片
# 即創(chuàng)建長度10,容量0的切片 score := make([]int, 0, 10)
- 切片后可以通過append賦值
- 切片可以重新切片,然后通過索引賦值
- 切片超過容量會自動增大,自身倍數(shù)
package main import ( "fmt" ) func main() { var scores [10]int scores[0] = 99 fmt.Printf("scoers:%d\n", scores) socres_english := [4]int{99, 100, 100, 99} for index, value := range socres_english { fmt.Printf("%d\t %d\n", index, value) } score_math := make([]int, 0, 10) score_math = append(score_math, 100) fmt.Println(score_math) fmt.Println(score_math[0]) score_math = score_math[0:8] score_math[7] = 99 fmt.Println(score_math) c := cap(score_math) fmt.Println(c) for i := 0; i < 25; i++ { score_math = append(score_math, i) if cap(score_math) != c { c = cap(score_math) fmt.Println(c) } } }
輸出:
Type 'dlv help' for list of commands.
scoers:[99 0 0 0 0 0 0 0 0 0]
0?? ? 99
1?? ? 100
2?? ? 100
3?? ? 99
[100]
100
[100 0 0 0 0 0 0 99]
10
20
40
Process 20448 has exited with status 0
Detaching
dlv dap (7204) exited with code: 0
3.映射Map
就好比其他語言中的 hash 表或者字典。它們的工作方式就是:定義鍵和值,并且可以獲取,設(shè)置和刪除其中的值。
同樣通過make方法創(chuàng)建。
package main import ( "fmt" ) func main() { lookup := make(map[string]int) lookup["goku"] = 9001 power, exists := lookup["vegeta"] fmt.Println(power, exists) total := len(lookup) fmt.Println(total) delete(lookup, "goku") fmt.Println(len(lookup)) }
Starting: C:\Users\livingbody\go\bin\dlv.exe dap --check-go-version=false --listen=127.0.0.1:14812 from c:\Users\livingbody\goworkspace\hello
DAP server listening at: 127.0.0.1:14812
Type 'dlv help' for list of commands.
0 false
1
0
Process 924 has exited with status 0
Detaching
dlv dap (700) exited with code: 0
迭代
for key, value := range lookup { ... }
注意:
map迭代沒有順序
原文鏈接:https://juejin.cn/post/7119383510036529159
相關(guān)推薦
- 2022-05-27 python使用pandas進(jìn)行量化回測_python
- 2023-02-27 C++文件讀取的4種情況匯總_C 語言
- 2024-01-05 IIS WebApi: 文件上傳,大小限制,提示413 (Request Entity Too La
- 2022-10-17 使用docker部署django的詳細(xì)步驟_docker
- 2022-07-22 C/C++冒泡排序
- 2023-11-18 Python將字符串String轉(zhuǎn)換成要使用的變量
- 2022-04-20 Android實現(xiàn)將View轉(zhuǎn)化為圖片并保存到本地_Android
- 2022-10-15 Nginx如何配置加密證書訪問實現(xiàn)_nginx
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支