Skip to content

Commit

Permalink
精校:7.4
Browse files Browse the repository at this point in the history
  • Loading branch information
unknwon committed Jul 30, 2015
1 parent f47e8de commit 640517d
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 36 deletions.
2 changes: 1 addition & 1 deletion README_gc.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ Golang 编程:245386165

|更新日期 |更新内容
|----------|------------------
|2015-07-27|7.1 声明和初始化
|2015-07-30|7.4 切片重组(reslice)
2 changes: 1 addition & 1 deletion eBook/07.3.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,4 @@ b) 写一个 SumAndAverage 方法,返回两个 int 和 float32 类型的未命

- [目录](directory.md)
- 上一节:[切片](07.2.md)
- 下一节:[切片重组](07.4.md)
- 下一节:[切片重组(reslice)](07.4.md)
73 changes: 42 additions & 31 deletions eBook/07.4.md
Original file line number Diff line number Diff line change
@@ -1,37 +1,44 @@
# 7.4 切片重组
# 7.4 切片重组(reslice)

我们已经知道 slice 创建的时候通常比相关数组小,例如
我们已经知道切片创建的时候通常比相关数组小,例如

slice1 := make([]type, start_length, capacity)
```go
slice1 := make([]type, start_length, capacity)
```

其中 start_length 作为 slice 初始长度而 capacity 作为相关数组的长度。
其中 `start_length` 作为切片初始长度而 `capacity` 作为相关数组的长度。

这么做的好处是我们的 slice 在达到容量上限后可以扩容。改变 slice 长度的过程称之为切片重组 **reslicing**,做法如下:`slice1 = slice1[0:end]`,其中 end 是新的末尾索引(即长度)。
这么做的好处是我们的切片在达到容量上限后可以扩容。改变切片长度的过程称之为切片重组 **reslicing**,做法如下:`slice1 = slice1[0:end]`,其中 end 是新的末尾索引(即长度)。

将 slice 扩展 1 位可以这么做:`sl = sl[0:len(sl)+1]`
将切片扩展 1 位可以这么做:

slice 可以反复扩展直到占据整个相关数组。
```go
sl = sl[0:len(sl)+1]
```

切片可以反复扩展直到占据整个相关数组。

示例 7.11 [reslicing.go](examples/chapter_7/reslicing.go)

package main
import "fmt"

func main() {
slice1 := make([]int, 0, 10)
// load the slice, cap(slice1) is 10:
for i := 0; i < cap(slice1); i++ {
slice1 = slice1[0:i+1]
slice1[i] = i
fmt.Printf("The length of slice is %d\n", len(slice1))
}

// print the slice:
for i := 0; i < len(slice1); i++ {
fmt.Printf("Slice at %d is %d\n", i, slice1[i])
}
}
```go
package main
import "fmt"

func main() {
slice1 := make([]int, 0, 10)
// load the slice, cap(slice1) is 10:
for i := 0; i < cap(slice1); i++ {
slice1 = slice1[0:i+1]
slice1[i] = i
fmt.Printf("The length of slice is %d\n", len(slice1))
}

// print the slice:
for i := 0; i < len(slice1); i++ {
fmt.Printf("Slice at %d is %d\n", i, slice1[i])
}
}
```

输出结果:

Expand All @@ -58,18 +65,22 @@ slice 可以反复扩展直到占据整个相关数组。

另一个例子:

var ar = [10]int{0,1,2,3,4,5,6,7,8,9}
var a = ar[5:7] // reference to subarray {5,6} - len(a) is 2 and cap(a) is 5
```go
var ar = [10]int{0,1,2,3,4,5,6,7,8,9}
var a = ar[5:7] // reference to subarray {5,6} - len(a) is 2 and cap(a) is 5
```

将a重新分片
将 a 重新分片:

a = a[0:4] // ref of subarray {5,6,7,8} - len(a) is now 4 but cap(a) is still 5
```go
a = a[0:4] // ref of subarray {5,6,7,8} - len(a) is now 4 but cap(a) is still 5
```

问题 7.7
**问题 7.7**

1) 如果 a 是一个 slice,那么 s[n:n] 的长度是多少?
1) 如果 a 是一个切片,那么 `s[n:n]` 的长度是多少?

2) s[n:n+1] 的长度又是多少?
2) `s[n:n+1]` 的长度又是多少?

## 链接

Expand Down
2 changes: 1 addition & 1 deletion eBook/07.5.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,5 @@ func AppendByte(slice []byte, data ...byte) []byte {
## 链接

- [目录](directory.md)
- 上一节:[切片重组](07.4.md)
- 上一节:[切片重组(reslice)](07.4.md)
- 下一节:[字符串、数组和切片的应用](07.6.md)
4 changes: 2 additions & 2 deletions eBook/directory.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
- 5.6 [标签与 goto](05.6.md)
- 第6章:[函数(function)](06.0.md)
- 6.1 [介绍](06.1.md)
- 6.2 [参数与返回值](06.2.md)
- 6.2 [函数参数与返回值](06.2.md)
- 6.3 [传递变长参数](06.3.md)
- 6.4 [defer 和追踪](06.4.md)
- 6.5 [内置函数](06.5.md)
Expand All @@ -62,7 +62,7 @@
- 7.1 [声明和初始化](07.1.md)
- 7.2 [切片](07.2.md)
- 7.3 [For-range 结构](07.3.md)
- 7.4 [切片重组](07.4.md)
- 7.4 [切片重组(reslice)](07.4.md)
- 7.5 [切片的复制与追加](07.5.md)
- 7.6 [字符串、数组和切片的应用](07.6.md)
- 第8章:[Maps](08.0.md)
Expand Down

0 comments on commit 640517d

Please sign in to comment.