This commit is contained in:
Unknown
2014-06-08 11:58:12 -04:00
parent f8550edc9a
commit 2ee37bd330
3 changed files with 175 additions and 18 deletions

View File

@@ -9,7 +9,7 @@
## 翻译进度 ## 翻译进度
5.3 [switch 结构](eBook/05.3.md) 5.4 [for 结构](eBook/05.4.md)
## 支持本书 ## 支持本书

View File

@@ -1,20 +1,3 @@
## 啊哦,亲,你看得也太快了。。。还没翻译完呢 0 0
要不等到 **2014 年 6 月 8 日** 再来看看吧~~
这里还有一些其它的学习资源噢~
- [《Go编程基础》](https://github.com/Unknwon/go-fundamental-programming)
- [《Go Web编程》](https://github.com/astaxie/build-web-application-with-golang)
- [《Go名库讲解》](https://github.com/Unknwon/go-rock-libraries-showcases)
神马?你说你不想学习?那好吧,去逛逛看看行情也行~
- [Go Walker](https://gowalker.org) **Go 项目 API 文档在线浏览工具**
- [Go 中国社区](http://bbs.go-china.org)
- [Go语言学习园地](http://studygolang.com/)
- [Golang中国](http://golangtc.com)
# 5.4 for 结构 # 5.4 for 结构
如果想要重复执行某些语句Go 语言中您只有 for 结构可以使用。不要小看它,这个 for 结构比其它语言中的更为灵活。 如果想要重复执行某些语句Go 语言中您只有 for 结构可以使用。不要小看它,这个 for 结构比其它语言中的更为灵活。
@@ -254,3 +237,159 @@ for t, err = p.Token(); err == nil; t, err = p.Token() {
## 5.4.4 for-range 结构 ## 5.4.4 for-range 结构
这是 Go 特有的一种的迭代结构您会发现它在许多情况下都非常有用它可以迭代任何一个集合包括数组和 map详见第 7 8 )。语法上很类似其它语言中 foreach 语句但您依旧可以获得每次迭代所对应的索引一般形式为`for ix, val := range coll { }`
要注意的是`val` 始终为集合中对应索引的值拷贝因此它一般只具有只读性质对它所做的任何修改都不会影响到集合中原有的值** 译者注如果 `val` 为指针则会产生指针的拷贝依旧可以修改集合中的原值 **)。一个字符串是 Unicode 编码的字符或称之为 `rune`集合因此您也可以用它迭代字符串
```
for pos, char := range str {
...
}
```
每个 rune 字符和索引在 for-range 循环中是一一对应的它能够自动根据 UTF-8 规则识别 Unicode 编码的字符
Listing 5.9 [range_string.go](examples/chapter_5/range_string.go)
```
package main
import "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: 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: 18
character C starts at byte position 0
character h starts at byte position 1
character i starts at byte position 2
character n starts at byte position 3
character e starts at byte position 4
character s starts at byte position 5
character e starts at byte position 6
character : starts at byte position 7
character starts at byte position 8
character 日 starts at byte position 9
character 本 starts at byte position 12
character 語 starts at byte position 15
index int(rune) rune char bytes
0 67 U+0043 'C' 43
1 104 U+0068 'h' 68
2 105 U+0069 'i' 69
3 110 U+006E 'n' 6E
4 101 U+0065 'e' 65
5 115 U+0073 's' 73
6 101 U+0065 'e' 65
7 58 U+003A ':' 3A
8 32 U+0020 ' ' 20
9 26085 U+65E5 '日' E6 97 A5
12 26412 U+672C '本' E6 9C AC
15 35486 U+8A9E '語' E8 AA 9E
```
请将输出结果和 Listing 5.7for_string.go进行对比
我们可以看到常用英文字符使用 1 个字节表示而中文字符使用 3 个字符表示
**练习 5.9** 以下程序的输出结果是什么
```
for i := 0; i < 5; i++ {
var v int
fmt.Printf("%d ", v)
v = 5
}
```
**问题 5.2** 请描述以下 for 循环的输出结果
1.
```
for i := 0; ; i++ {
fmt.Println("Value of i is now:", i)
}
```
2.
```
for i := 0; i < 3; {
fmt.Println("Value of i:", i)
}
```
3.
```
s := ""
for ; s != "aaaaa"; {
fmt.Println("Value of s:", s)
s = s + "a"
}
```
4.
```
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)
}
```
## 链接
- [目录](directory.md)
- 上一节[switch 结构](05.3.md)
- 下一节[Break 与 continue](05.5.md)

18
eBook/05.5.md Normal file
View File

@@ -0,0 +1,18 @@
## 啊哦,亲,你看得也太快了。。。还没翻译完呢 0 0
要不等到 **2014 年 6 月 10 日** 再来看看吧~~
这里还有一些其它的学习资源噢~
- [《Go编程基础》](https://github.com/Unknwon/go-fundamental-programming)
- [《Go Web编程》](https://github.com/astaxie/build-web-application-with-golang)
- [《Go名库讲解》](https://github.com/Unknwon/go-rock-libraries-showcases)
神马?你说你不想学习?那好吧,去逛逛看看行情也行~
- [Go Walker](https://gowalker.org) **Go 项目 API 文档在线浏览工具**
- [Go 中国社区](http://bbs.go-china.org)
- [Go语言学习园地](http://studygolang.com/)
- [Golang中国](http://golangtc.com)
# 5.5 Break 与 continue