淺談Go切片的值修改是否會覆蓋數(shù)組的值?
切片與數(shù)組
數(shù)組
數(shù)組是具有相同 唯一類型 的一組以編號且長度固定的數(shù)據(jù)項序列
數(shù)組聲明
var identifier [len]type
切片
切片(slice)是對數(shù)組一個連續(xù)片段的引用,切片是一個引用類型,切片是一個指針。
切片是一個長度可變的數(shù)組。
切片聲明
var identifier []type
切片初始化
var slice1 []type = arr[start:end]
切片的值修改
修改切片的值覆蓋數(shù)組的值
代碼
package main
import "fmt"
func main() {
? arr := [5]int{1,2,3,4,5}
? fmt.Printf("slice modification before: array=%v len=%d cap=%d\n", arr, len(arr), cap(arr))
??
? s := arr[0:3]
? fmt.Printf("len=%d cap=%d ptr=%p slice=%v\n", len(s), cap(s), s, s)
? s = append(s, 6,10)?
??
? fmt.Printf("len=%d cap=%d ptr=%p slice=%v\n", len(s), cap(s), s, s)
? fmt.Printf("slice modification: array=%v len=%d cap=%d\n", arr, len(arr), cap(arr))
}結果
slice modification before: array=[1 2 3 4 5] len=5 cap=5
len=3 cap=5 ptr=0xc00000c300 slice=[1 2 3]
len=5 cap=5 ptr=0xc00000c300 slice=[1 2 3 6 10]
slice modification: array=[1 2 3 6 10] len=5 cap=5
由于未超出底層數(shù)組的容量,地址不變,數(shù)組還是原來的數(shù)組,所以修改切片會覆蓋數(shù)組的值。
修改切片不覆蓋數(shù)組的值
代碼
package main
import "fmt"
func main() {
? arr := [5]int{1,2,3,4,5}
? fmt.Printf("slice modification before: array=%v len=%d cap=%d\n", arr, len(arr), cap(arr))
??
? s := arr[0:3]
? fmt.Printf("len=%d cap=%d ptr=%p slice=%v\n", len(s), cap(s), s, s)
? s = append(s, 6,10,11)?
??
? fmt.Printf("len=%d cap=%d ptr=%p slice=%v\n", len(s), cap(s), s, s)
? fmt.Printf("slice modification: array=%v len=%d cap=%d\n", arr, len(arr), cap(arr))
}結果
slice modification before: array=[1 2 3 4 5] len=5 cap=5
len=3 cap=5 ptr=0xc00000c300 slice=[1 2 3]
len=6 cap=10 ptr=0xc0000141e0 slice=[1 2 3 6 10 11]
slice modification: array=[1 2 3 4 5] len=5 cap=5
超出底層數(shù)組的容量,地址變了,會分配一個新的數(shù)組,返回的切片指向這個新數(shù)組,舊的數(shù)組的值未被修改。
切片的擴容機制
切片小數(shù)1024
代碼
package main
import "fmt"
func main() {
? arr := [5]int{1,2,3,4,5}
??
? s := arr[0:3]
? fmt.Printf("len=%d cap=%d ptr=%p slice=%v\n", len(s), cap(s), s, s)
? s = append(s, 6,10,11)?
??
? fmt.Printf("len=%d cap=%d ptr=%p slice=%v\n", len(s), cap(s), s, s)
}結果
before: len=3 cap=5 ptr=0xc00000c300 slice=[1 2 3]
after: len=6 cap=10 ptr=0xc0000141e0 slice=[1 2 3 6 10 11]
該切片的容量為源切片容量的2倍
切片不小于1024
代碼
package main
import "fmt"
func main() {
? arr := [1024]int{1,2,3,...,1024}
? s := arr[0:]?
? fmt.Printf("before: len=%d cap=%d ptr=%p slice=%v\n", len(s), cap(s), s, s)
? s = append(s, 1025)
? fmt.Printf("after: len=%d cap=%d ptr=%p slice=%v\n", len(s), cap(s), s, s)
}結果
before: len=1024 cap=1024 ptr=0xc000112000 slice=[1 2 3 ... 1024]
after: len=1025 cap=1280 ptr=0xc00012c000 slice=[1 2 3 ... 1024 1025]
切片容量在原來的切片的容量上增加了1/4
切片源碼
如果切片的容量不夠會調用growslice這個函數(shù)進行擴容
// ?go1.16.6 src/runtime/slice.go
func growslice(et *_type, old slice, cap int) slice {
? ? ... // code
? ? newcap := old.cap
? ? doublecap := newcap + newcap
? ? if cap > doublecap {
? ? ? ? newcap = cap
? ? } else {
? ? ? ? if old.cap < 1024 {
? ? ? ? ? ? newcap = doublecap
? ? ? ? } else {
? ? ? ? ? ? // Check 0 < newcap to detect overflow
? ? ? ? ? ? // and prevent an infinite loop.
? ? ? ? ? ? for 0 < newcap && newcap < cap {
? ? ? ? ? ? ? ? newcap += newcap / 4
? ? ? ? ? ? }
? ? ? ? ? ? // Set newcap to the requested cap when
? ? ? ? ? ? // the newcap calculation overflowed.
? ? ? ? ? ? if newcap <= 0 {
? ? ? ? ? ? ? ? newcap = cap
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? // 根據(jù)切片類型和容量計算要分配內存的大小
? ? var overflow bool
? ? var lenmem, newlenmem, capmem uintptr
? ? switch {
? ? ... // code
? ? }
? ? ... // code
? ? // 將舊切片的數(shù)據(jù)搬到新切片開辟的地址中
? ? memmove(p, old.array, lenmem)
? ? return slice{p, old.len, newcap}
}切片擴容的規(guī)則
- 如果擴容之后,還沒有觸及原數(shù)組的容量,則切片中的指針指向的還是原數(shù)組,如果擴容后超過了原數(shù)組的容量,則開辟一塊新的內存,把原來的值拷貝過來,這種情況絲毫不會影響到原數(shù)組。
- 如果切片的容量小于 1024,則擴容時其容量大小乘以2;一旦容量大小超過 1024,則增長因子變成 1.25,即每次增加原來容量的四分之一。
到此這篇關于淺談Go切片的值修改是否會覆蓋數(shù)組的值 的文章就介紹到這了,更多相關Go切片覆蓋數(shù)組的值 內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
golang實現(xiàn)整型和字節(jié)數(shù)組之間的轉換操作
這篇文章主要介紹了golang實現(xiàn)整型和字節(jié)數(shù)組之間的轉換操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12
GoLang之使用Context控制請求超時的實現(xiàn)
這篇文章主要介紹了GoLang之使用Context控制請求超時的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-04-04

