国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

腳本之家,腳本語言編程技術及教程分享平臺!
分類導航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務器之家 - 腳本之家 - Golang - 深入理解Go語言中的數組和切片

深入理解Go語言中的數組和切片

2020-05-01 13:08daisy Golang

Go語言中的數組大概相當與C/C++中的數組,固定大小,不能夠動態擴展大小,而切片大概相當與C++中的Vector,可以動態擴展大小,當大小超過容量時,重新分配一塊內存,然后將數據復制到新的內存區域。下面我們通過幾個問題來更

一、類型

數組是值類型,將一個數組賦值給另一個數組時,傳遞的是一份拷貝。

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

我們來看一段代碼

?
1
2
3
4
5
6
7
8
9
10
11
12
13
//a是一個數組,注意數組是一個固定長度的,初始化時候必須要指定長度,不指定長度的話就是切片了
a := [3]int{1, 2, 3}
//b是數組,是a的一份拷貝
b := a
//c是切片,是引用類型,底層數組是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, 所以下面一段代碼生成了一個slice,是引用類型

?
1
2
3
4
5
6
7
8
9
10
11
12
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]

三、當對slice append 超出底層數組的界限時

?
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
//n1是n2的底層數組
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
 
//對n2執行append操作后,n2超出了底層數組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

四、引用“失效”

實現了刪除slice最后一個item的函數

?
1
2
3
4
5
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)
}

調用此函數后,發現原來的slice并沒有改變

?
1
2
3
4
5
6
7
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]
}

打印出來的結果如下:

?
1
2
3
4
5
[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的指針值,因為slice是引用類型,所以指針值都是相同的,我們換成打印slice的地址看下

?
1
2
3
4
5
6
7
8
9
10
11
12
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]
}

結果:

?
1
2
3
4
5
[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作為函數參數傳入函數時,實際上也是拷貝了一份slice,因為slice本身是個指針,所以從現象來看,slice是引用類型

總結

以上就是這篇文章的全部內容,希望對大家的學習或者工作帶來一定的幫助,如果有疑問大家可以留言交流。

延伸 · 閱讀

精彩推薦
  • Golanggo日志系統logrus顯示文件和行號的操作

    go日志系統logrus顯示文件和行號的操作

    這篇文章主要介紹了go日志系統logrus顯示文件和行號的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧...

    SmallQinYan12302021-02-02
  • Golanggolang的httpserver優雅重啟方法詳解

    golang的httpserver優雅重啟方法詳解

    這篇文章主要給大家介紹了關于golang的httpserver優雅重啟的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,...

    helight2992020-05-14
  • GolangGolang通脈之數據類型詳情

    Golang通脈之數據類型詳情

    這篇文章主要介紹了Golang通脈之數據類型,在編程語言中標識符就是定義的具有某種意義的詞,比如變量名、常量名、函數名等等,Go語言中標識符允許由...

    4272021-11-24
  • Golanggo語言制作端口掃描器

    go語言制作端口掃描器

    本文給大家分享的是使用go語言編寫的TCP端口掃描器,可以選擇IP范圍,掃描的端口,以及多線程,有需要的小伙伴可以參考下。 ...

    腳本之家3642020-04-25
  • Golanggolang如何使用struct的tag屬性的詳細介紹

    golang如何使用struct的tag屬性的詳細介紹

    這篇文章主要介紹了golang如何使用struct的tag屬性的詳細介紹,從例子說起,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看...

    Go語言中文網11352020-05-21
  • Golanggolang 通過ssh代理連接mysql的操作

    golang 通過ssh代理連接mysql的操作

    這篇文章主要介紹了golang 通過ssh代理連接mysql的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧...

    a165861639710342021-03-08
  • Golanggolang json.Marshal 特殊html字符被轉義的解決方法

    golang json.Marshal 特殊html字符被轉義的解決方法

    今天小編就為大家分享一篇golang json.Marshal 特殊html字符被轉義的解決方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧 ...

    李浩的life12792020-05-27
  • GolangGolang中Bit數組的實現方式

    Golang中Bit數組的實現方式

    這篇文章主要介紹了Golang中Bit數組的實現方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧...

    天易獨尊11682021-06-09
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25 Weibo Article 26 Weibo Article 27 Weibo Article 28 Weibo Article 29 Weibo Article 30 Weibo Article 31 Weibo Article 32 Weibo Article 33 Weibo Article 34 Weibo Article 35 Weibo Article 36 Weibo Article 37 Weibo Article 38 Weibo Article 39 Weibo Article 40
主站蜘蛛池模板: www中文字幕 | 国产成人免费视频 | 欧美精品成人一区二区三区四区 | 亚洲好看站 | 中文日韩在线 | 91精品国产色综合久久 | 久草视频免费看 | 国产一区二区三区在线免费 | 日韩午夜电影 | 中文字幕亚洲一区 | 91视频免费网站 | 国产日韩欧美三级 | 欧美成人高清视频 | 久久精品二区 | 玖玖在线 | 日韩午夜 | 欧美日韩精品一区 | 日本不卡一区 | 欧美精品不卡 | 国产亚洲精品精品国产亚洲综合 | 国产精品1区2区 | 亚洲一区二区三区四区的 | 亚洲高清在线视频 | www.欧美| 亚洲欧美综合乱码精品成人网 | 美日韩视频 | 在线一区| 91丝袜| 快色视频在线观看 | 久久精品一区二区三区四区 | 欧美精品一二三区 | yy6080一级二级 | av在线一区二区三区 | 特黄视频 | 成人欧美一区二区三区在线观看 | 欧美激情高清 | 成人在线一区二区 | 亚洲国产一区二区三区精品 | 成人久久久 | 希岛爱理一区二区三区av高清 | 亚洲精品免费在线观看 |