Go 语言循环语句
如果想要重复执行某些语句,Go 语言中您只有 for 结构可以使用。不要小看它,这个 for 结构比其它语言中的更为灵活。本章节我们要了解的就是循环语句的各种结构,以及如何跳出循环:
注意:其它许多语言中也没有发现和 do while 完全对等的 for 结构,可能是因为这种需求并不是那么强烈。
for-range结构
这是 Go 特有的一种的迭代结构,您会发现它在许多情况下都非常有用。它可以迭代任何一个集合(包括数组和 map)。语法上很类似其它语言中 foreach 语句,不同的是,for-range可以获得每次迭代所对应的索引。
一般形式为:
for ix, val := range coll { }。
要注意的是,val 始终为集合中对应索引的值拷贝,因此它一般只具有只读性质,对它所做的任何修改都不会影响到集合中原有的值(如果 val 为指针,则会产生指针的拷贝,可以修改集合中的原值)。一个字符串是 Unicode 编码的字符集合,因此您也可以用它迭代字符串:
for pos, char := range str {...}
每个 rune 字符和索引在 for-range 循环中是一一对应的。它能够自动根据 UTF-8 规则识别 Unicode 编码的字符。
示例:
package mainimport "fmt"func main() {str := "Go is a beautiful language!"fmt.Printf("The length of str is: %d\n", len(str))for pos, char := range str {fmt.Printf("Character on position %d is: %c \n", pos, char)}fmt.Println()str2 := "Chinese: 汉语"fmt.Printf("The length of str2 is: %d\n", len(str2))for pos, char := range str2 {fmt.Printf("character %c starts at byte position %d\n", char, pos)}fmt.Println()fmt.Println("index int(rune) rune char bytes")for index, rune := range str2 {fmt.Printf("%-2d %d %U '%c' % X\n", index, rune, rune, rune, []byte(string(rune)))}}
输出:
The length of str is: 27Character on position 0 is: GCharacter on position 1 is: oCharacter on position 2 is:Character on position 3 is: iCharacter on position 4 is: sCharacter on position 5 is:Character on position 6 is: aCharacter on position 7 is:Character on position 8 is: bCharacter on position 9 is: eCharacter on position 10 is: aCharacter on position 11 is: uCharacter on position 12 is: tCharacter on position 13 is: iCharacter on position 14 is: fCharacter on position 15 is: uCharacter on position 16 is: lCharacter on position 17 is:Character on position 18 is: lCharacter on position 19 is: aCharacter on position 20 is: nCharacter on position 21 is: gCharacter on position 22 is: uCharacter on position 23 is: aCharacter on position 24 is: gCharacter on position 25 is: eCharacter on position 26 is: !The length of str2 is: 18character C starts at byte position 0character h starts at byte position 1character i starts at byte position 2character n starts at byte position 3character e starts at byte position 4character s starts at byte position 5character e starts at byte position 6character : starts at byte position 7character starts at byte position 8character 汉 starts at byte position 9character 语 starts at byte position 12index int(rune) rune char bytes0 67 U+0043 'C' 431 104 U+0068 'h' 682 105 U+0069 'i' 693 110 U+006E 'n' 6E4 101 U+0065 'e' 655 115 U+0073 's' 736 101 U+0065 'e' 657 58 U+003A ':' 3A8 32 U+0020 ' ' 209 26085 U+65E5 '汉' E6 97 A512 26412 U+672C '语' E6 9C AC
我们可以看到,常用英文字符使用 1 个字节表示,而汉字使用 3 个字符表示。
在数组 与 切片 中的应用
for ix, value := range slice1 {...}
ix 是数组或者切片的索引,value 是在该索引位置的值,他们都是仅在 for 循环内部可见的局部变量。value 只是 slice1 的 ix 索引位置的值的一个拷贝,不能用来修改 slice1 该索引位置的值。
示例 slices_forrange.go
package mainimport "fmt"func main() {var slice1 []int = make([]int, 4)slice1[0] = 1slice1[1] = 2slice1[2] = 3slice1[3] = 4for ix, value := range slice1 {fmt.Printf("Slice at %d is: %d\n", ix, value)}}
示例 slices_forrange2.go
package mainimport "fmt"func main() {seasons := []string{"Spring", "Summer", "Autumn", "Winter"}for ix, season := range seasons {fmt.Printf("Season %d is: %s\n", ix, season)}var season stringfor _, season = range seasons {fmt.Printf("%s\n", season)}}
示例slicesforrange2.go 给出了一个关于字符串的例子, `` 可以用于忽略索引。
如果你只需要索引,你可以忽略第二个变量:
for ix := range seasons {fmt.Printf("%d", ix)}// Output: 0 1 2 3
多维切片下的 for-range
for row := range screen {for column := range screen[row] {screen[row][column] = 1}}
练习:写一个 Sum 函数,传入参数为一个 32 位 float 组成的数组 arrF,返回该数组的所有数字和。如果把数组修改为切片的话代码要做怎样的修改?如果用切片形式方法实现不同长度数组的的和呢?