diff --git a/README.md b/README.md index b4edb92..18711e8 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ 该翻译版本已获得原作者(Ivo Balbaert)本人授权,并表示支持开源事业的发展! ##翻译进度 -第5章:[控制结构](eBook/05.0.md) +5.1 [if-else 结构](eBook/05.1.md) ##支持本书 如果你喜欢本书《Go入门指南》,你可以参与到本书的翻译或纠正工作中来,具体请联系【无闻 E-mail:joe2010xtmf#163.com】,一同完善本书并帮助壮大 Go 语言在国内的学习群体,给大家提供更好的学习资源。 diff --git a/eBook/05.1.md b/eBook/05.1.md index a071c10..f6fcdb6 100644 --- a/eBook/05.1.md +++ b/eBook/05.1.md @@ -1,18 +1,3 @@ -##啊哦,亲,你看得也太快了。。。还没翻译完呢 0 0 -要不等到 ***2013 年 6 月 24 日*** 再来看看吧~~ - -这里还有一些其它的学习资源噢~ - - - [《Go编程基础》](https://github.com/Unknwon/go-fundamental-programming):已更新至 [第12课](https://github.com/Unknwon/go-fundamental-programming/blob/master/lectures/lecture12.md) - - [《Go Web编程》](https://github.com/astaxie/build-web-application-with-golang) - -神马?你说你不想学习?那好吧,去逛逛看看行情也行~ - -- [Go Walker](http://gowalker.org) **Go 项目文档在线浏览工具** -- [Golang中文社区](http://bbs.mygolang.com/forum.php) -- [Go语言学习园地](http://studygolang.com/) -- [Golang中国](http://golang.tc) - #5.1 if-else 结构 if 是用于测试某个条件(布尔型或逻辑型)的语句,如果该条件成立,则会执行 if 后由大括号括起来的代码块,否则就忽略该代码块继续执行后续的代码。 @@ -53,12 +38,12 @@ else-if 分支的数量是没有限制的,但是为了代码的可读性,还 要注意的是,在你使用 `gofmt` 格式化代码之后,每个分支内的代码都会缩进 4 个或 8 个空格,或者是 1 个 tab,并且右大括号与对应的 if 关键字垂直对齐。 -CHECK -有时,条件两边的括号()是可以省略的,当条件比较复杂时,使用括号()可以让代码更清楚.条件可以是复合条件,使用逻辑操作符&&, || 和!,通过使用()可以强制优先级,提高代码可读性. +在有些情况下,条件语句两侧的括号是可以被省略的;当条件比较复杂时,则可以使用括号让代码更易读。条件允许是符合条件,需使用 &&、|| 或 !,你可以使用括号来提升某个表达式的运算优先级,并提高代码的可读性。 + +一种可能用到条件语句的场景是测试变量的值,在不同的情况执行不同的语句,不过将在第 5.3 节讲到的 switch 结构会更适合这种情况。 + +Example 5.1 [booleans.go](examples/chapter_5/booleans.go) -一种可能的应用是测试变量值,在不同的情况下,执行不同的语句,但是5.3章节讲到的switch语句更适合这种场景. -
-Listing 5.1 - booleans.go: package main import “fmt” func main() { @@ -69,137 +54,126 @@ Listing 5.1 - booleans.go: fmt.Printf(“The value is false\n”) } } - // Output: The value is true --注意 不需要判断if bool1 == true,因为bool1已经是一个boolean类型的值. -当测试true和有利条件时,几乎都正常,但是有可能需要判断反向用!(not): if !bool1 或者 if !(condition). 在最后的列子中,条件两边的括号()常常是必须的,例如: if !(var1 == var2). +输出: + + The value is true + +**注意事项** 这里不需要使用 `if bool1 == true` 来判断,因为 `bool1` 本身已经是一个布尔类型的值。 + +这种做法一般都用在测试 `true` 或者有利条件时,但你也可以使用取反 `!` 来判断值的相反结果,如:`if !bool1` 或者 `if !(condition)`。后者的括号大多数情况下是必须的,如这种情况:`if !(var1 == var2)`。 + +当 if 结构内有 break、continue、goto 或者 return 语句时,Go 代码的常见写法是省略 else 部分(另见第 5.2 节)。无论满足哪个条件都会返回 x 或者 y 时,一般使用以下写法: -如果if结构内有break,continue,goto或return语句时,可以省略else子句.(见5.2章节).条件是否成立,返回不同的值x和y时: -
if condition { return x } return y --标记: 不要同时在if else的两个分支里都使用return,将会编译报错:"function ends without a return statement"(这是一个编译BUG) -一些有用的例子: -(1)判断一个字符串是否为空: -
- if str == "" { ... } - or - if len(str) == 0 {...} -+**注意事项** 不要同时在 if-else 结构的两个分支里都使用 return 语句,这将导致编译报错 “function ends without a return statement”(你可以认为这是一个编译器的 Bug 或者特性)。( ***译者注:该问题已经在 Go 1.1 中被修复或者说改进*** ) -(2)判断运行Go程序的操作系统类型 -可以通过运行时常量来判断.GOOS(见2.2) -
- if runtime.GOOS == "windows" { - ... - } else { // Unix - like - ... - } -+这里举一些有用的例子: + +1. 判断一个字符串是否为空:`if str == "" { ... }` 或 `if len(str) == 0 {...}`。 +2. 判断运行 Go 程序的操作系统类型,这可以通过常量 `runtime.GOOS` 来判断(第 2.2 节)。 -init()方法是适合执行这段代码的地方.这是一个代码片段, 改变输入结束的提示. -
- var prompt = "Enter a digit, e.g. 3 "+ "or %s to quit." - - func init() { if runtime.GOOS == "windows" { - prompt = fmt.Sprintf(prompt, "Ctrl+Z, Enter") - } else { //Unix-like - prompt = fmt.Sprintf(prompt, "Ctrl+D") + ... + } else { // Unix - like + ... } - } --(3)一个Abs()方法,返回一个整型数字的绝对值: -
- func Abs(x int) int { - if x < 0 { - return -x + 这段代码一般被放在 init() 函数中执行。这儿还有一段示例来演示如何根据操作系统来决定输入结束的提示: + + var prompt = "Enter a digit, e.g. 3 "+ "or %s to quit." + + func init() { + if runtime.GOOS == "windows" { + prompt = fmt.Sprintf(prompt, "Ctrl+Z, Enter") + } else { //Unix-like + prompt = fmt.Sprintf(prompt, "Ctrl+D") + } } - return x - } --(4) 一个比较两个整型数字大小的方法: -
-func isGreater(x, y int) bool { - if x > y { - return true - } - return false -} -+3. 函数 `Abs()` 用于返回一个整型数字的绝对值: + + func Abs(x int) int { + if x < 0 { + return -x + } + return x + } + +4. `isGreater` 用于比较两个整型数字的大小: + + func isGreater(x, y int) bool { + if x > y { + return true + } + return false + } + +在第四种情况中,if 可以包含一个初始化语句(如:给一个变量赋值)。这种写法具有固定的格式(在初始化语句后方必须加上分号): + + if initialization; condition { + // do something + } -在第四种情况,if可以以一个初始化语句开始(如给一个变量赋值).固定的格式(在初始化语句后必须加上分号;): -
-if initialization; condition { - // do something -} -例如: -
-val := 10 -if val > max { - // do something -} --你可以这样写: -
-if val := 10; val > max { - // do something -} --但是注意,简洁的初始化变量 := 方式声明的变量只存在与if结构中.(变量范围只在if{}的大括号之间,如果有if结构有else子句时,在else子句中变量也存在):如果变量在if结构之前就已经存在,那么在if结构中,该变量原来的值会被隐藏.一个简单的解决办法就是不在初始化中使用:=(见5.2的例3 怎么使用) - -Listing 5.2-ifelse.go: -
-package main - -import "fmt" - -func main() { - var first int = 10 - var cond int - - if first <= 0 { - - fmt.Printf("first is less than or equal to 0\n") - } else if first > 0 && first < 5 { - - fmt.Printf("first is between 0 and 5\n") - } else { - - fmt.Printf("first is 5 or greater\n") + val := 10 + if val > max { + // do something } - if cond = 5; cond > 10 { - fmt.Printf("cond is greater than 10\n") - } else { +你也可以这样写: - fmt.Printf("cond is not greater than 10\n") + if val := 10; val > max { + // do something } -} -Output: first is 5 or greater cond is not greater than 10 -+但要注意的是,使用简短方式 `:=` 声明的变量的作用域只存在于 if 结构中(在 if 结构的大括号之间,如果使用 if-else 结构则在 else 代码块中变量也会存在)。如果变量在 if 结构之前就已经存在,那么在 if 结构中,该变量原来的值会被隐藏。最简单的解决方案就是不要在初始化语句中声明变量(见 5.2 节的例 3 了解更多)。 -The following code-snippet shows how the result of a function process( ) can be retrieved in the if, -and action taken according to the value: -
-if value := process(data); value > max { - ... -if value := process(data); value > max { - ... -} -+Example 5.2 [ifelse.go](examples/chapter_5/ifelse.go) + package main + + import "fmt" + + func main() { + var first int = 10 + var cond int + + if first <= 0 { + + fmt.Printf("first is less than or equal to 0\n") + } else if first > 0 && first < 5 { + + fmt.Printf("first is between 0 and 5\n") + } else { + + fmt.Printf("first is 5 or greater\n") + } + if cond = 5; cond > 10 { + + fmt.Printf("cond is greater than 10\n") + } else { + + fmt.Printf("cond is not greater than 10\n") + } + } +输出: + + first is 5 or greater cond is not greater than 10 + +下面的代码片段展示了如何通过在初始化语句中获取函数 `process()` 的返回值,并在条件语句中作为判定条件来决定是否执行 if 结构中的代码: + + if value := process(data); value > max { + ... + if value := process(data); value > max { + ... + } ##链接 - [目录](directory.md) diff --git a/eBook/05.2.md b/eBook/05.2.md new file mode 100644 index 0000000..9b68091 --- /dev/null +++ b/eBook/05.2.md @@ -0,0 +1,21 @@ +##啊哦,亲,你看得也太快了。。。还没翻译完呢 0 0 +要不等到 ***2013 年 6 月 25 日*** 再来看看吧~~ + +这里还有一些其它的学习资源噢~ + + - [《Go编程基础》](https://github.com/Unknwon/go-fundamental-programming):已更新至 [第13课](https://github.com/Unknwon/go-fundamental-programming/blob/master/lectures/lecture12.md) + - [《Go Web编程》](https://github.com/astaxie/build-web-application-with-golang) + +神马?你说你不想学习?那好吧,去逛逛看看行情也行~ + +- [Go Walker](http://gowalker.org) **Go 项目文档在线浏览工具** +- [Golang中文社区](http://bbs.mygolang.com/forum.php) +- [Go语言学习园地](http://studygolang.com/) +- [Golang中国](http://golang.tc) + +#5.2 测试多返回值函数的错误 + +##链接 +- [目录](directory.md) +- 上一节:[if-else 结构](05.1.md) +- 下一节: [switch 结构](05.3.md) \ No newline at end of file