詳解Go語(yǔ)言中數(shù)組,切片和映射的使用
Arrays (數(shù)組), Slices (切片) 和 Maps (映射) 是常見(jiàn)的一類數(shù)據(jù)結(jié)構(gòu)
1.Arrays (數(shù)組)
- 數(shù)組是定長(zhǎng)的。
- 長(zhǎng)度不可改變。
初始化
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)建長(zhǎng)度10,容量0的切片 score := make([]int, 0, 10)
- 切片后可以通過(guò)append賦值
- 切片可以重新切片,然后通過(guò)索引賦值
- 切片超過(guò)容量會(huì)自動(dòng)增大,自身倍數(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
就好比其他語(yǔ)言中的 hash 表或者字典。它們的工作方式就是:定義鍵和值,并且可以獲取,設(shè)置和刪除其中的值。
同樣通過(guò)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迭代沒(méi)有順序
到此這篇關(guān)于詳解Go語(yǔ)言中數(shù)組,切片和映射的使用的文章就介紹到這了,更多相關(guān)Go數(shù)組 切片 映射內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
go內(nèi)存隊(duì)列l(wèi)ist VS slice實(shí)現(xiàn)方式對(duì)比分析
這篇文章主要為大家介紹了go內(nèi)存隊(duì)列l(wèi)ist VS slice實(shí)現(xiàn)方式對(duì)比分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08Golang實(shí)現(xiàn)組合模式和裝飾模式實(shí)例詳解
這篇文章主要介紹了Golang實(shí)現(xiàn)組合模式和裝飾模式,本文介紹組合模式和裝飾模式,golang實(shí)現(xiàn)兩種模式有共同之處,但在具體應(yīng)用場(chǎng)景有差異。通過(guò)對(duì)比兩個(gè)模式,可以加深理解,需要的朋友可以參考下2022-11-11Go 語(yǔ)言數(shù)據(jù)結(jié)構(gòu)之雙鏈表學(xué)習(xí)教程
這篇文章主要為大家介紹了Go 語(yǔ)言數(shù)據(jù)結(jié)構(gòu)之雙鏈表學(xué)習(xí)教程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08golang如何實(shí)現(xiàn)mapreduce單進(jìn)程版本詳解
這篇文章主要給大家介紹了關(guān)于golang如何實(shí)現(xiàn)mapreduce單進(jìn)程版本的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2018-01-01Golang map實(shí)踐及實(shí)現(xiàn)原理解析
這篇文章主要介紹了Golang map實(shí)踐以及實(shí)現(xiàn)原理,Go 語(yǔ)言中,通過(guò)哈希查找表實(shí)現(xiàn) map,用鏈表法解決哈希沖突,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2022-06-06使用Golong實(shí)現(xiàn)JWT身份驗(yàn)證的詳細(xì)過(guò)程
JWT提供了一種強(qiáng)大而靈活的方法來(lái)處理Web應(yīng)用程序中的身份驗(yàn)證和授權(quán),本教程將引導(dǎo)您逐步實(shí)現(xiàn)Go應(yīng)用程序中的JWT身份驗(yàn)證過(guò)程,感興趣的朋友跟隨小編一起看看吧2024-03-03