切片定義
切片(Slice)是一個(gè)擁有相同類型元素的可變長度的序列。它是基于數(shù)組類型做的一層封裝。它非常靈活折欠,支持自動(dòng)擴(kuò)容。切片是一個(gè)引用類型,它的內(nèi)部結(jié)構(gòu)包含地址、長度和容量耳璧。切片一般用于快速地操作一塊數(shù)據(jù)集合。
數(shù)組與切片
切片的數(shù)據(jù)實(shí)際是通過數(shù)組來保存的酱床,每個(gè)切片都有三個(gè)信息:底層數(shù)組的指針、切片的長度(len)和切片的容量(cap)趟佃。
舉個(gè)栗子扇谣,底層數(shù)組a := [8]int{0, 1, 2, 3, 4, 5, 6, 7}昧捷;
-
切片s1 := a[:5],切片和數(shù)組對(duì)應(yīng)關(guān)系:
-
切片s2 := a[3:6]罐寨,切片和數(shù)組對(duì)應(yīng)關(guān)系:
指向同一個(gè)底層數(shù)組的切片修改值
切片是指向底層數(shù)組的引用類型靡挥,指向同一個(gè)底層數(shù)組的切片底層數(shù)據(jù)存放都是在同一個(gè)位置,修改某個(gè)切片會(huì)影響到在同一個(gè)范圍的切片
import (
"fmt"
"testing"
)
func TestSliceShareMemory(t *testing.T) {
year := []string{"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}
Q2 := year[3:6]
t.Log(Q2, len(Q2), cap(Q2))
summer := year[5:8]
t.Log(summer, len(summer), cap(summer))
summer[0] = "Unkonw"
t.Log(Q2)
t.Log(year)
}
// === RUN TestSliceShareMemory
// TestSliceShareMemory: slice_test.go:36: [Apr May Jun] 3 9
// TestSliceShareMemory: slice_test.go:38: [Jun Jul Aug] 3 7
// TestSliceShareMemory: slice_test.go:40: [Apr May Unkonw]
// TestSliceShareMemory: slice_test.go:41: [Jan Feb Mar Apr May Unkonw Jul Aug Sep Oct Nov Dec]
// --- PASS: TestSliceShareMemory (0.00s)
// PASS
切片表達(dá)式
切片表達(dá)式從字符串衩茸、數(shù)組芹血、指向數(shù)組或切片的指針構(gòu)造子字符串或切片。它有兩種變體:一種指定low和high兩個(gè)索引界限值的簡單的形式楞慈,另一種是除了low和high索引界限值外還指定容量的完整的形式:
- 切片len()是可訪問長度幔烛,容量cap()是總空間大小。通過數(shù)組生成的切片囊蓝, len為首尾索引之差饿悬,cap為從切片首索引到數(shù)組末尾長度
- 切片s[low:high:max],從切片s的low處到high處所獲得的切片聚霜,len=high-low狡恬,cap=max-low
func TestSliceExpression(t *testing.T) {
a := [5]int{1, 2, 3, 4, 5}
// b := a[1:3:7]
b := a[1:3:5]
fmt.Printf("b:%v len(b):%v cap(b):%v\n", b, len(b), cap(b))
}
// === RUN TestSliceExpression
// b:[2 3] len(b):2 cap(b):4
// --- PASS: TestSliceExpression (0.00s)
// PASS
切片不能比較
兩個(gè)切片不能直接比較,會(huì)報(bào)錯(cuò):
func TestSliceCompare(t *testing.T) {
a := []int{1, 2, 3, 4}
b := []int{1, 2, 3, 4}
if a == b {
t.Log("a==b")
}
}
// invalid operation: a == b (slice can only be compared to nil)
// FAIL go_learn/go_test/slice_test [build failed]
// FAIL
切片的append
切片通過append()添加元素時(shí)蝎宇,未超過newcap時(shí)底層數(shù)組地址不變弟劲,超過的話底層數(shù)組會(huì)申請新的內(nèi)存地址。新申請的容量大小計(jì)算分成了兩步姥芥,有關(guān)append的源碼在$GOROOT/src/runtime/slice.go
兔乞,可以自己去分析。
計(jì)算邏輯newcap
- new cap > old * 2直接申請新容量大小;
- 小于2倍時(shí)凉唐,len<1024翻倍庸追,len>1024加上1/4
//$GOROOT/src/runtime/slice.go
newcap := old.cap
doublecap := newcap + newcap
if cap > doublecap {
newcap = cap
} else {
if old.len < 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
}
}
}
//...略
lenmem = uintptr(old.len)
newlenmem = uintptr(cap)
capmem = roundupsize(uintptr(newcap))
overflow = uintptr(newcap) > maxAlloc
newcap = int(capmem)
//...略
實(shí)際申請內(nèi)存大小
上面先算了個(gè)邏輯上的newcap,實(shí)際申請內(nèi)存的時(shí)候台囱,由于內(nèi)存對(duì)齊的關(guān)系不會(huì)直接就用newcap淡溯。上面的代碼就是在算好了newcap后會(huì)調(diào)用roundupsize()
得到實(shí)際的大小。
//$GOROOT/src/runtime/msize.go
// Returns size of the memory block that mallocgc will allocate if you ask for the size.
func roundupsize(size uintptr) uintptr {
if size < _MaxSmallSize {
if size <= smallSizeMax-8 {
return uintptr(class_to_size[size_to_class8[(size+smallSizeDiv-1)/smallSizeDiv]])
} else {
return uintptr(class_to_size[size_to_class128[(size-smallSizeMax+largeSizeDiv-1)/largeSizeDiv]])
}
}
if size+_PageSize < size {
return size
}
return alignUp(size, _PageSize)
}
在roundupsize()
中的class_to_size簿训、size_to_class8是存了具體大小的數(shù)組咱娶,根據(jù)傳入的newcap來算出下標(biāo),拿到對(duì)應(yīng)的大小值强品。這些數(shù)組在//$GOROOT/src/runtime/sizeclasses.go
豺总,這個(gè)文件又是//$GOROOT/src/runtime/mksizeclasses.go.go
生成的,生成規(guī)則就先不去看了择懂。
//$GOROOT/src/runtime/sizeclasses.go
// Code generated by mksizeclasses.go; DO NOT EDIT.
//go:generate go run mksizeclasses.go
var class_to_size = [_NumSizeClasses]uint16{0, 8, 16, 32, 48, 64, 80, 96, 112, 128 ...}
var size_to_class8 = [smallSizeMax/smallSizeDiv + 1]uint8{0, 1, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, 14, 14, 15, 15, 16, 16, 17, 17, 18, 18, 18, 18, 19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 22, 22, 22, 22, 23, 23, 23, 23, 24, 24, 24, 24, 25 ...}
了解了上面的內(nèi)容之后就可以理解下面的幾個(gè)例子了:
func TestSliceAppend(t *testing.T) {
var a = make([]int, 5, 10)
for i := 0; i < 10; i++ {
a = append(a, i)
fmt.Printf("a ptr: %p\n", a)
}
fmt.Println(a)
}
// === RUN TestSliceAppend
// a ptr: 0xc00000c320
// a ptr: 0xc00000c320
// a ptr: 0xc00000c320
// a ptr: 0xc00000c320
// a ptr: 0xc00000c320
// a ptr: 0xc0000100a0
// a ptr: 0xc0000100a0
// a ptr: 0xc0000100a0
// a ptr: 0xc0000100a0
// a ptr: 0xc0000100a0
// [0 0 0 0 0 0 1 2 3 4 5 6 7 8 9]
// --- PASS: TestSliceAppend (0.00s)
// PASS
func TestSliceAppend2(t *testing.T) {
s := []int{1, 2, 3, 4}
a := make([]int, 3, 6)
b := append(a, 10)
a[0] = 50
fmt.Printf("a: %v\tptr: %p\tfirst: %v\n", a, a, a[0])
fmt.Printf("b: %v\tptr: %p\tfirst: %v\n", b, b, b[0])
b = append(a, s...)
a[0] = 100
fmt.Printf("a: %v\tptr: %p\tfirst: %v\n", a, a, a[0])
fmt.Printf("b: %v\tptr: %p\tfirst: %v\n", b, b, b[0])
}
// === RUN TestSliceAppend2
// a: [50 0 0] ptr: 0xc00000a330 first: 50
// b: [50 0 0 10] ptr: 0xc00000a330 first: 50
// a: [100 0 0] ptr: 0xc00000a330 first: 100
// b: [50 0 0 1 2 3 4] ptr: 0xc00001a4e0 first: 50
// --- PASS: TestSliceAppend2 (0.00s)
// PASS
func TestSliceAppend3(t *testing.T) {
a1 := make([]int, 20)
b1 := make([]int, 40)
a1 = append(a1, b1...)
fmt.Println(len(a1), cap(a1))
a2 := make([]int, 20)
b2 := make([]int, 42)
a2 = append(a2, b2...)
fmt.Println(len(a2), cap(a2))
}
// === RUN TestSliceAppend3
// 60 60
// 62 64
// --- PASS: TestSliceAppend3 (0.00s)
// PASS
切片元素刪除
在要?jiǎng)h除的元素左右切兩下:a1 = append(a1[:1], a1[2:]...),刪除其實(shí)是將所刪元素后面的往前挪另玖。
func TestSliceDelete(t *testing.T) {
a := []int{30, 31, 32, 33, 34, 35, 36, 37}
// 要?jiǎng)h除索引為2的元素
a = append(a[:2], a[3:]...)
t.Log(a)
}
// === RUN TestSliceDelete
// TestSliceDelete: slice_test.go:98: [30 31 33 34 35 36 37]
// --- PASS: TestSliceDelete (0.00s)
// PASS
參考內(nèi)容
- Go語言基礎(chǔ)之切片
- golang源碼
- 測試的幾個(gè)例子