mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-11 22:06:51 +08:00
@@ -84,19 +84,19 @@ slice 在内存中的组织方式实际上是一个有 3 个域的结构体:
|
||||
//slice1 = slice1[0:7 ] // panic: runtime error: slice bound out of range
|
||||
}
|
||||
|
||||
输出:
|
||||
Slice at 0 is 2
|
||||
Slice at 1 is 3
|
||||
Slice at 2 is 4
|
||||
The length of arr1 is 6
|
||||
The length of slice1 is 3
|
||||
The capacity of slice1 is 4
|
||||
Slice at 0 is 2
|
||||
Slice at 1 is 3
|
||||
Slice at 2 is 4
|
||||
Slice at 3 is 5
|
||||
The length of slice1 is 4
|
||||
The capacity of slice1 is 4
|
||||
输出:
|
||||
Slice at 0 is 2
|
||||
Slice at 1 is 3
|
||||
Slice at 2 is 4
|
||||
The length of arr1 is 6
|
||||
The length of slice1 is 3
|
||||
The capacity of slice1 is 4
|
||||
Slice at 0 is 2
|
||||
Slice at 1 is 3
|
||||
Slice at 2 is 4
|
||||
Slice at 3 is 5
|
||||
The length of slice1 is 4
|
||||
The capacity of slice1 is 4
|
||||
|
||||
如果 s2 是一个 slice,你可以将 s2 向后移动一位 `s2 = s2[1:]`,但是末尾没有移动。slice 只能向后移动,`s2 = s2[-1:]` 会导致编译错误。slice 不能被重新分片以获取数组的前一个元素。
|
||||
|
||||
@@ -162,19 +162,19 @@ make 的使用方式是:`func make([]T, len, cap)` 其中 cap 是可选参数
|
||||
fmt.Printf("The capacity of slice1 is %d\n", cap(slice1))
|
||||
}
|
||||
|
||||
输出结构:
|
||||
Slice at 0 is 0
|
||||
Slice at 1 is 5
|
||||
Slice at 2 is 10
|
||||
Slice at 3 is 15
|
||||
Slice at 4 is 20
|
||||
Slice at 5 is 25
|
||||
Slice at 6 is 30
|
||||
Slice at 7 is 35
|
||||
Slice at 8 is 40
|
||||
Slice at 9 is 45
|
||||
The length of slice1 is 10
|
||||
The capacity of slice1 is 10
|
||||
输出结构:
|
||||
Slice at 0 is 0
|
||||
Slice at 1 is 5
|
||||
Slice at 2 is 10
|
||||
Slice at 3 is 15
|
||||
Slice at 4 is 20
|
||||
Slice at 5 is 25
|
||||
Slice at 6 is 30
|
||||
Slice at 7 is 35
|
||||
Slice at 8 is 40
|
||||
Slice at 9 is 45
|
||||
The length of slice1 is 10
|
||||
The capacity of slice1 is 10
|
||||
|
||||
因为字符串是纯粹不可变的字节数组,它们也可以被切分成 slice。
|
||||
|
||||
|
Reference in New Issue
Block a user