mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-12 02:16:48 +08:00
精校:5.4
This commit is contained in:
152
eBook/05.4.md
152
eBook/05.4.md
@@ -10,9 +10,9 @@
|
||||
|
||||
for 初始化语句; 条件语句; 修饰语句 {}
|
||||
|
||||
Listing 5.6 [for1.go](examples/chapter_5/for1.go):
|
||||
示例 5.6 [for1.go](examples/chapter_5/for1.go):
|
||||
|
||||
```
|
||||
```go
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
@@ -26,13 +26,11 @@ func main() {
|
||||
|
||||
输出:
|
||||
|
||||
```
|
||||
This is the 0 iteration
|
||||
This is the 1 iteration
|
||||
This is the 2 iteration
|
||||
This is the 3 iteration
|
||||
This is the 4 iteration
|
||||
```
|
||||
This is the 0 iteration
|
||||
This is the 1 iteration
|
||||
This is the 2 iteration
|
||||
This is the 3 iteration
|
||||
This is the 4 iteration
|
||||
|
||||
由花括号括起来的代码块会被重复执行已知次数,该次数是根据计数器(此例为 i)决定的。循环开始前,会执行且仅会执行一次初始化语句 `i := 0;`;这比在循环之前声明更为简短。紧接着的是条件语句 `i < 5;`,在每次循环开始前都会进行判断,一旦判断结果为 false,则退出循环体。最后一部分为修饰语句 `i++`,一般用于增加或减少计数器。
|
||||
|
||||
@@ -44,13 +42,15 @@ This is the 4 iteration
|
||||
|
||||
您还可以在循环中同时使用多个计数器:
|
||||
|
||||
for i, j := 0, N; i < j; i, j = i+1, j-1 {}
|
||||
```go
|
||||
for i, j := 0, N; i < j; i, j = i+1, j-1 {}
|
||||
```
|
||||
|
||||
这得益于 Go 语言具有的平行赋值的特性(可以查看第 7 章 string_reverse.go 中反转数组的示例)。
|
||||
|
||||
您可以将两个 for 循环嵌套起来:
|
||||
|
||||
```
|
||||
```go
|
||||
for i:=0; i<5; i++ {
|
||||
for j:=0; j<10; j++ {
|
||||
println(j)
|
||||
@@ -60,9 +60,9 @@ for i:=0; i<5; i++ {
|
||||
|
||||
如果您使用 for 循环迭代一个 Unicode 编码的字符串,会发生什么?
|
||||
|
||||
Listing 5.7 [for_string.go](examples/chapter_5/for_string.go):
|
||||
示例 5.7 [for_string.go](examples/chapter_5/for_string.go):
|
||||
|
||||
```
|
||||
```go
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
@@ -83,46 +83,44 @@ func main() {
|
||||
|
||||
输出:
|
||||
|
||||
```
|
||||
The length of str is: 27
|
||||
Character on position 0 is: G
|
||||
Character on position 1 is: o
|
||||
Character on position 2 is:
|
||||
Character on position 3 is: i
|
||||
Character on position 4 is: s
|
||||
Character on position 5 is:
|
||||
Character on position 6 is: a
|
||||
Character on position 7 is:
|
||||
Character on position 8 is: b
|
||||
Character on position 9 is: e
|
||||
Character on position 10 is: a
|
||||
Character on position 11 is: u
|
||||
Character on position 12 is: t
|
||||
Character on position 13 is: i
|
||||
Character on position 14 is: f
|
||||
Character on position 15 is: u
|
||||
Character on position 16 is: l
|
||||
Character on position 17 is:
|
||||
Character on position 18 is: l
|
||||
Character on position 19 is: a
|
||||
Character on position 20 is: n
|
||||
Character on position 21 is: g
|
||||
Character on position 22 is: u
|
||||
Character on position 23 is: a
|
||||
Character on position 24 is: g
|
||||
Character on position 25 is: e
|
||||
Character on position 26 is: !
|
||||
The length of str2 is: 9
|
||||
Character on position 0 is: æ
|
||||
Character on position 1 is:
|
||||
Character on position 2 is: ¥
|
||||
Character on position 3 is: æ
|
||||
Character on position 4 is:
|
||||
Character on position 5 is: ¬
|
||||
Character on position 6 is: è
|
||||
Character on position 7 is: ª
|
||||
Character on position 8 is:
|
||||
```
|
||||
The length of str is: 27
|
||||
Character on position 0 is: G
|
||||
Character on position 1 is: o
|
||||
Character on position 2 is:
|
||||
Character on position 3 is: i
|
||||
Character on position 4 is: s
|
||||
Character on position 5 is:
|
||||
Character on position 6 is: a
|
||||
Character on position 7 is:
|
||||
Character on position 8 is: b
|
||||
Character on position 9 is: e
|
||||
Character on position 10 is: a
|
||||
Character on position 11 is: u
|
||||
Character on position 12 is: t
|
||||
Character on position 13 is: i
|
||||
Character on position 14 is: f
|
||||
Character on position 15 is: u
|
||||
Character on position 16 is: l
|
||||
Character on position 17 is:
|
||||
Character on position 18 is: l
|
||||
Character on position 19 is: a
|
||||
Character on position 20 is: n
|
||||
Character on position 21 is: g
|
||||
Character on position 22 is: u
|
||||
Character on position 23 is: a
|
||||
Character on position 24 is: g
|
||||
Character on position 25 is: e
|
||||
Character on position 26 is: !
|
||||
The length of str2 is: 9
|
||||
Character on position 0 is: æ
|
||||
Character on position 1 is:
|
||||
Character on position 2 is: ¥
|
||||
Character on position 3 is: æ
|
||||
Character on position 4 is:
|
||||
Character on position 5 is: ¬
|
||||
Character on position 6 is: è
|
||||
Character on position 7 is: ª
|
||||
Character on position 8 is:
|
||||
|
||||
如果我们打印 str 和 str2 的长度,会分别得到 27 和 9。
|
||||
|
||||
@@ -139,14 +137,12 @@ Character on position 8 is:
|
||||
|
||||
创建一个程序,要求能够打印类似下面的结果(直到每行 25 个字符时为止):
|
||||
|
||||
```
|
||||
G
|
||||
GG
|
||||
GGG
|
||||
GGGG
|
||||
GGGGG
|
||||
GGGGGG
|
||||
```
|
||||
G
|
||||
GG
|
||||
GGG
|
||||
GGGG
|
||||
GGGGG
|
||||
GGGGGG
|
||||
|
||||
1. 使用 2 层嵌套 for 循环。
|
||||
2. 使用一层 for 循环以及字符串截断。
|
||||
@@ -171,7 +167,7 @@ for 结构的第二种形式是没有头部的条件判断迭代(类似其它
|
||||
|
||||
Listing 5.8 [for2.go](examples/chapter_5/for2.go):
|
||||
|
||||
```
|
||||
```go
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
@@ -188,14 +184,12 @@ func main() {
|
||||
|
||||
输出:
|
||||
|
||||
```
|
||||
The variable i is now: 4
|
||||
The variable i is now: 3
|
||||
The variable i is now: 2
|
||||
The variable i is now: 1
|
||||
The variable i is now: 0
|
||||
The variable i is now: -1
|
||||
```
|
||||
The variable i is now: 4
|
||||
The variable i is now: 3
|
||||
The variable i is now: 2
|
||||
The variable i is now: 1
|
||||
The variable i is now: 0
|
||||
The variable i is now: -1
|
||||
|
||||
## 5.4.3 无限循环
|
||||
|
||||
@@ -209,7 +203,7 @@ The variable i is now: -1
|
||||
|
||||
无限循环的经典应用是服务器,用于不断等待和接受新的请求。
|
||||
|
||||
```
|
||||
```go
|
||||
for t, err = p.Token(); err == nil; t, err = p.Token() {
|
||||
...
|
||||
}
|
||||
@@ -221,7 +215,7 @@ for t, err = p.Token(); err == nil; t, err = p.Token() {
|
||||
|
||||
要注意的是,`val` 始终为集合中对应索引的值拷贝,因此它一般只具有只读性质,对它所做的任何修改都不会影响到集合中原有的值(**译者注:如果 `val` 为指针,则会产生指针的拷贝,依旧可以修改集合中的原值**)。一个字符串是 Unicode 编码的字符(或称之为 `rune`)集合,因此您也可以用它迭代字符串:
|
||||
|
||||
```
|
||||
```go
|
||||
for pos, char := range str {
|
||||
...
|
||||
}
|
||||
@@ -229,9 +223,9 @@ for pos, char := range str {
|
||||
|
||||
每个 rune 字符和索引在 for-range 循环中是一一对应的。它能够自动根据 UTF-8 规则识别 Unicode 编码的字符。
|
||||
|
||||
Listing 5.9 [range_string.go](examples/chapter_5/range_string.go):
|
||||
示例 5.9 [range_string.go](examples/chapter_5/range_string.go):
|
||||
|
||||
```
|
||||
```go
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
@@ -323,7 +317,7 @@ index int(rune) rune char bytes
|
||||
|
||||
**练习 5.9** 以下程序的输出结果是什么?
|
||||
|
||||
```
|
||||
```go
|
||||
for i := 0; i < 5; i++ {
|
||||
var v int
|
||||
fmt.Printf("%d ", v)
|
||||
@@ -335,7 +329,7 @@ for i := 0; i < 5; i++ {
|
||||
|
||||
1.
|
||||
|
||||
```
|
||||
```go
|
||||
for i := 0; ; i++ {
|
||||
fmt.Println("Value of i is now:", i)
|
||||
}
|
||||
@@ -343,7 +337,7 @@ for i := 0; ; i++ {
|
||||
|
||||
2.
|
||||
|
||||
```
|
||||
```go
|
||||
for i := 0; i < 3; {
|
||||
fmt.Println("Value of i:", i)
|
||||
}
|
||||
@@ -351,7 +345,7 @@ for i := 0; i < 3; {
|
||||
|
||||
3.
|
||||
|
||||
```
|
||||
```go
|
||||
s := ""
|
||||
for ; s != "aaaaa"; {
|
||||
fmt.Println("Value of s:", s)
|
||||
@@ -361,7 +355,7 @@ for ; s != "aaaaa"; {
|
||||
|
||||
4.
|
||||
|
||||
```
|
||||
```go
|
||||
for i, j, s := 0, 5, "a"; i < 3 && j < 100 && s != "aaaaa"; i, j,
|
||||
s = i+1, j+1, s + "a" {
|
||||
fmt.Println("Value of i, j, s:", i, j, s)
|
||||
|
Reference in New Issue
Block a user