Go語(yǔ)言中數(shù)組與切片的區(qū)別是什么

Go語(yǔ)言中數(shù)組與切片的區(qū)別是什么?針對(duì)這個(gè)問題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡(jiǎn)單易行的方法。

為珠山等地區(qū)用戶提供了全套網(wǎng)頁(yè)設(shè)計(jì)制作服務(wù),及珠山網(wǎng)站建設(shè)行業(yè)解決方案。主營(yíng)業(yè)務(wù)為網(wǎng)站制作、成都網(wǎng)站設(shè)計(jì)、珠山網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠(chéng)的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長(zhǎng)期合作。這樣,我們也可以走得更遠(yuǎn)!

一、類型

數(shù)組是值類型,將一個(gè)數(shù)組賦值給另一個(gè)數(shù)組時(shí),傳遞的是一份拷貝。

切片是引用類型,切片包裝的數(shù)組稱為該切片的底層數(shù)組。

我們來看一段代碼

//a是一個(gè)數(shù)組,注意數(shù)組是一個(gè)固定長(zhǎng)度的,初始化時(shí)候必須要指定長(zhǎng)度,不指定長(zhǎng)度的話就是切片了
a := [3]int{1, 2, 3}
//b是數(shù)組,是a的一份拷貝
b := a
//c是切片,是引用類型,底層數(shù)組是a
c := a[:]
for i := 0; i < len(a); i++ {
 a[i] = a[i] + 1
}
//改變a的值后,b是a的拷貝,b不變,c是引用,c的值改變
fmt.Println(a) //[2,3,4]
fmt.Println(b) //[1 2 3]
fmt.Println(c) //[2,3,4]

二、make

make 只能用于slice, map channel, 所以下面一段代碼生成了一個(gè)slice,是引用類型

s1 := make([]int, 0, 3)

for i := 0; i < cap(s1); i++ {
 s1 = append(s1, i)
}
s2 := s1
for i := 0; i < len(a); i++ {
 s1[i] = s1[i] + 1
}

fmt.Println(s1) //[1 2 3]
fmt.Println(s2) //[1 2 3]

三、當(dāng)對(duì)slice append 超出底層數(shù)組的界限時(shí)

//n1是n2的底層數(shù)組
n1 := [3]int{1, 2, 3}
n2 := n1[0:3]
fmt.Println("address of items in n1: ")
for i := 0; i < len(n1); i++ {
 fmt.Printf("%p\n", &n1[i])
}
//address of items in n1:
//0xc20801e160
//0xc20801e168
//0xc20801e170
fmt.Println("address of items in n2: ")
for i := 0; i < len(n2); i++ {
 fmt.Printf("%p\n", &n2[i])
}
//address of items in n2:
//0xc20801e160
//0xc20801e168
//0xc20801e170

//對(duì)n2執(zhí)行append操作后,n2超出了底層數(shù)組n1的j
n2 = append(n2, 1)
fmt.Println("address of items in n1: ")
for i := 0; i < len(n1); i++ {
 fmt.Printf("%p\n", &n1[i])
}
//address of items in n1:
//0xc20801e160
//0xc20801e168
//0xc20801e170

fmt.Println("address of items in n2: ")
for i := 0; i < len(n2); i++ {
 fmt.Printf("%p\n", &n2[i])
}
//address of items in n2:
//0xc20803a2d0
//0xc20803a2d8
//0xc20803a2e0
//0xc20803a2e8

四、引用“失效”

實(shí)現(xiàn)了刪除slice最后一個(gè)item的函數(shù)

func rmLast(a []int) {
 fmt.Printf("[rmlast] the address of a is %p", a)
 a = a[:len(a)-1]
 fmt.Printf("[rmlast] after remove, the address of a is %p", a)
}

調(diào)用此函數(shù)后,發(fā)現(xiàn)原來的slice并沒有改變

func main() {
 xyz := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
 fmt.Printf("[main] the address of xyz is %p\n", xyz)
 rmLast(xyz)
 fmt.Printf("[main] after remove, the address of xyz is %p\n", xyz)
 fmt.Printf("%v", xyz) //[1 2 3 4 5 6 7 8 9]
}

打印出來的結(jié)果如下:

[main] the address of xyz is 0xc2080365f0
[rmlast] the address of a is 0xc2080365f0
[rmlast] after remove, the address of a is 0xc2080365f0
[main] after remove, the address of xyz is 0xc2080365f0
[1 2 3 4 5 6 7 8 9]

這里直接打印了slice的指針值,因?yàn)?code>slice是引用類型,所以指針值都是相同的,我們換成打印slice的地址看下

func rmLast(a []int) {
 fmt.Printf("[rmlast] the address of a is %p", &a)
 a = a[:len(a)-1]
 fmt.Printf("[rmlast] after remove, the address of a is %p", &a)
}
func main() {
 xyz := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
 fmt.Printf("[main] the address of xyz is %p\n", &xyz)
 rmLast(xyz)
 fmt.Printf("[main] after remove, the address of xyz is %p\n", &xyz)
 fmt.Printf("%v", xyz) //[1 2 3 4 5 6 7 8 9]
}

結(jié)果:

[main] the address of xyz is 0xc20801e1e0
[rmlast] the address of a is 0xc20801e200
[rmlast] after remove, the address of a is 0xc20801e200
[main] after remove, the address of xyz is 0xc20801e1e0
[1 2 3 4 5 6 7 8 9]

這次可以看到slice作為函數(shù)參數(shù)傳入函數(shù)時(shí),實(shí)際上也是拷貝了一份slice,因?yàn)?code>slice本身是個(gè)指針,所以從現(xiàn)象來看,slice是引用類型

的學(xué)習(xí)或者工作帶來一定的幫助,如果有疑問大家可以留言交流。

關(guān)于Go語(yǔ)言中數(shù)組與切片的區(qū)別是什么問題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識(shí)。

新聞標(biāo)題:Go語(yǔ)言中數(shù)組與切片的區(qū)別是什么
本文地址:http://bm7419.com/article16/goepdg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App開發(fā)、標(biāo)簽優(yōu)化、移動(dòng)網(wǎng)站建設(shè)靜態(tài)網(wǎng)站、ChatGPT品牌網(wǎng)站設(shè)計(jì)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

搜索引擎優(yōu)化