mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-12 07:34:06 +08:00
@@ -9,3 +9,8 @@
|
|||||||
其他的关键词像break和continue可以改变循环.
|
其他的关键词像break和continue可以改变循环.
|
||||||
还有return关键词,/*从语句主体中直接返回*/和goto关键词跳转执行label所在处的代码
|
还有return关键词,/*从语句主体中直接返回*/和goto关键词跳转执行label所在处的代码
|
||||||
Go完全忽略了if,swaitch,for等周围的括号(),比java,c++,c#减少的视觉上的代码混乱,让代码看起来更简洁.
|
Go完全忽略了if,swaitch,for等周围的括号(),比java,c++,c#减少的视觉上的代码混乱,让代码看起来更简洁.
|
||||||
|
|
||||||
|
##链接
|
||||||
|
- [目录](directory.md)
|
||||||
|
- 上一章:[基本结构和基本数据类型](04.0.md)
|
||||||
|
- 下一节: [if else 结构](05.1.md)
|
||||||
|
187
eBook/05.1.md
Normal file
187
eBook/05.1.md
Normal file
@@ -0,0 +1,187 @@
|
|||||||
|
#5.1 if else 结构
|
||||||
|
if 判断一个条件(boolean类型或是否合理)语句:如果该条件成立,则执行if之后{}之间的语句,如果不成立if结构将被忽略,继续执行if之后的代码.
|
||||||
|
<pre>
|
||||||
|
if condition {
|
||||||
|
// do something
|
||||||
|
}
|
||||||
|
</pre>
|
||||||
|
在两个分支的情况下,可以在if condition{}之后添加else{}结构,当if condition条件不成立时,执行else{}之间的语句;if else两个独立的分支(只能有一个执行)
|
||||||
|
<pre>
|
||||||
|
if condition {
|
||||||
|
// do something
|
||||||
|
} else {
|
||||||
|
// do something
|
||||||
|
}
|
||||||
|
</pre>
|
||||||
|
在三个分支的情况下,可以在else之后加上if condition,那么我们有了三个独立的分支.
|
||||||
|
<pre>
|
||||||
|
if condition1 {
|
||||||
|
// do something
|
||||||
|
} else if condition2 {
|
||||||
|
// do something else
|
||||||
|
}else {
|
||||||
|
// catch-all or default
|
||||||
|
}
|
||||||
|
</pre>
|
||||||
|
else if 分支的数量是没有限制的,但是为了代码的可读性,还是不要在if 后面加入太多的else if结构.当使用这种形式时,把最有可能成立的条件,放在最前面.即使当{}之间只有一条语句时,{}也不可省略.(有些人不喜欢这样.从另一方面来说,这样代码更一致,符合主流的软件工程原则.)
|
||||||
|
if和else 之后的大括号{ 必须和if 或else 在同一行. else if 和 else 必须与他们前一个结构的结束大括号 } 在同一行.这些规则是强制性的,否则不能编译通过.
|
||||||
|
|
||||||
|
非法的Go代码:
|
||||||
|
if x{
|
||||||
|
}
|
||||||
|
else { //INVALID
|
||||||
|
}
|
||||||
|
|
||||||
|
注意每个分支被缩进了4(或者8)个空格或1个tab,并且结束大括号}和if是垂直对齐的;执行gofmt时,这些会自动执行.
|
||||||
|
|
||||||
|
有时,条件两边的括号()是可以省略的,当条件比较复杂时,使用括号()可以让代码更清楚.条件可以是复合条件,使用逻辑操作符&&, || 和!,通过使用()可以强制优先级,提高代码可读性.
|
||||||
|
|
||||||
|
一种可能的应用是测试变量值,在不同的情况下,执行不同的语句,但是5.3章节讲到的switch语句更适合这种场景.
|
||||||
|
<pre>
|
||||||
|
Listing 5.1 - booleans.go:
|
||||||
|
package main
|
||||||
|
import “fmt”
|
||||||
|
func main() {
|
||||||
|
bool1 := true
|
||||||
|
if bool1 {
|
||||||
|
fmt.Printf(“The value is true\n”)
|
||||||
|
} else {
|
||||||
|
fmt.Printf(“The value is false\n”)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Output: The value is true
|
||||||
|
</pre>
|
||||||
|
注意 不需要判断if bool1 == true,因为bool1已经是一个boolean类型的值.
|
||||||
|
|
||||||
|
当测试true和有利条件时,几乎都正常,但是有可能需要判断反向用!(not): if !bool1 或者 if !(condition). 在最后的列子中,条件两边的括号()常常是必须的,例如: if !(var1 == var2).
|
||||||
|
|
||||||
|
如果if结构内有break,continue,goto或return语句时,可以省略else子句.(见5.2章节).条件是否成立,返回不同的值x和y时:
|
||||||
|
<pre>
|
||||||
|
if condition {
|
||||||
|
return x
|
||||||
|
}
|
||||||
|
return y
|
||||||
|
</pre>
|
||||||
|
标记: 不要同时在if else的两个分支里都使用return,将会编译报错:"function ends without a return statement"(这是一个编译BUG)
|
||||||
|
|
||||||
|
一些有用的例子:
|
||||||
|
(1)判断一个字符串是否为空:
|
||||||
|
<pre>
|
||||||
|
if str == "" { ... }
|
||||||
|
or
|
||||||
|
if len(str) == 0 {...}
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
(2)判断运行Go程序的操作系统类型
|
||||||
|
可以通过运行时常量来判断.GOOS(见2.2)
|
||||||
|
<pre>
|
||||||
|
if runtime.GOOS == "windows" {
|
||||||
|
...
|
||||||
|
} else { // Unix - like
|
||||||
|
...
|
||||||
|
}
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
init()方法是适合执行这段代码的地方.这是一个代码片段, 改变输入结束的提示.
|
||||||
|
<pre>
|
||||||
|
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")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
(3)一个Abs()方法,返回一个整型数字的绝对值:
|
||||||
|
<pre>
|
||||||
|
func Abs(x int) int {
|
||||||
|
if x < 0 {
|
||||||
|
return -x
|
||||||
|
}
|
||||||
|
return x
|
||||||
|
}
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
(4) 一个比较两个整型数字大小的方法:
|
||||||
|
<pre>
|
||||||
|
func isGreater(x, y int) bool {
|
||||||
|
if x > y {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
在第四种情况,if可以以一个初始化语句开始(如给一个变量赋值).固定的格式(在初始化语句后必须加上分号;):
|
||||||
|
<pre>
|
||||||
|
if initialization; condition {
|
||||||
|
// do something
|
||||||
|
}
|
||||||
|
</pre>
|
||||||
|
例如:
|
||||||
|
<pre>
|
||||||
|
val := 10
|
||||||
|
if val > max {
|
||||||
|
// do something
|
||||||
|
}
|
||||||
|
</pre>
|
||||||
|
你可以这样写:
|
||||||
|
<pre>
|
||||||
|
if val := 10; val > max {
|
||||||
|
// do something
|
||||||
|
}
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
但是注意,简洁的初始化变量 := 方式声明的变量只存在与if结构中.(变量范围只在if{}的大括号之间,如果有if结构有else子句时,在else子句中变量也存在):如果变量在if结构之前就已经存在,那么在if结构中,该变量原来的值会被隐藏.一个简单的解决办法就是不在初始化中使用:=(见5.2的例3 怎么使用)
|
||||||
|
|
||||||
|
Listing 5.2-ifelse.go:
|
||||||
|
<pre>
|
||||||
|
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")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Output: first is 5 or greater cond is not greater than 10
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
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:
|
||||||
|
<pre>
|
||||||
|
if value := process(data); value > max {
|
||||||
|
...
|
||||||
|
if value := process(data); value > max {
|
||||||
|
...
|
||||||
|
}
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
##链接
|
||||||
|
- [目录](directory.md)
|
||||||
|
- 上一节:[控制结构](05.0.md)
|
||||||
|
- 下一节: [TODO](05.2.md)
|
Reference in New Issue
Block a user