增加流程控制和函数
This commit is contained in:
180
2.3.md
Normal file
180
2.3.md
Normal file
@@ -0,0 +1,180 @@
|
||||
#2.3 流程和函数
|
||||
这小节我们要介绍go里面的流程控制以及函数操作
|
||||
##流程控制
|
||||
流程控制是最伟大的发明了,因为有了他,你可以通过很简单的描述来表达很复杂的事情
|
||||
###if
|
||||
if语法也许是所有语言里面最常见的一种语法了,他的语法概括起来就是:`如果满足条件就做某事,否则做另一件事`
|
||||
|
||||
Go里面if条件语法中不需要括号,如下代码所示
|
||||
|
||||
if x > 10 {
|
||||
fmt.Println("x is greater than 10")
|
||||
} else {
|
||||
fmt.Println("x is less than 10")
|
||||
}
|
||||
|
||||
Go的if还有一个强大的地方就是条件里面允许什么一个变量,这个变量的作用域只能在该条件中,出了这个条件就不起作用了,如下所示
|
||||
|
||||
// 计算获取值x,然后根据x返回的大小,判断是否大于10.
|
||||
if x := computed_value(); x > 10 {
|
||||
fmt.Println("x is greater than 10")
|
||||
} else {
|
||||
fmt.Println("x is less than 10")
|
||||
}
|
||||
|
||||
//这个地方如果这样调用就编译出错了,因为x是条件里面的变量
|
||||
fmt.Println(x)
|
||||
|
||||
多个条件的时候如下所示
|
||||
|
||||
if integer == 3 {
|
||||
fmt.Println("The integer is equal to 3")
|
||||
} else if integer < 3 {
|
||||
fmt.Println("The integer is less than 3")
|
||||
} else {
|
||||
fmt.Println("The integer is greater than 3")
|
||||
}
|
||||
|
||||
###goto
|
||||
|
||||
Go有goto语句——明智的使用它。用goto跳转到一定是当前函数内定义的标签。例如假设这样一个循环:
|
||||
|
||||
|
||||
func myfunc() {
|
||||
i := 0
|
||||
Here: //这行的第一个词,以分号结束作为标签
|
||||
println(i)
|
||||
i++
|
||||
goto Here //跳转到Here去
|
||||
}
|
||||
|
||||
标签名是大小写敏感的。
|
||||
###for
|
||||
go里面最强大的一个控制逻辑就是for,他即可以用来循环读取数据,又可以当作while来控制逻辑,还能迭代操作。它的语法如下
|
||||
|
||||
for expression1; expression2; expression3{
|
||||
...
|
||||
}
|
||||
expression1、expression2、expression3都是表达式,其中expression1和expression3是变量申明或者函数调用返回值之类的,expression2是条件判断,expression1在循环开始之前调用,expression3在循环结束之后调用
|
||||
|
||||
一个例子比上面讲那么多更有用,那么我们看看下面的例子吧
|
||||
|
||||
package main
|
||||
import "fmt"
|
||||
|
||||
func main(){
|
||||
sum := 0;
|
||||
for index:=0; index < 10 ; index++ {
|
||||
sum += index
|
||||
}
|
||||
fmt.Println("sum is equal to ", sum)
|
||||
}
|
||||
//输出:sum is equal to 45
|
||||
|
||||
有些时候有多个需要操作的赋值操作,由于Go里面没有`,`操作,那么可以使用平行赋值`i, j = i+1, j-1`
|
||||
|
||||
|
||||
有些时候如果我们忽略expression1和expression3
|
||||
|
||||
sum := 1
|
||||
for ; sum < 1000; {
|
||||
sum += sum
|
||||
}
|
||||
|
||||
其中`;`也可以省略,那么就变成如下的代码了,是不是似曾相识,对,这就是while的功能
|
||||
|
||||
sum := 1
|
||||
for sum < 1000 {
|
||||
sum += sum
|
||||
}
|
||||
|
||||
在循环里面有两个关键操作`break`和`continue` ,`break`操作是跳出当前循环,`continue`是跳出本次循环,详细参考如下例子
|
||||
|
||||
for index := 10; index>0; index-- {
|
||||
if index == 5{
|
||||
break或者continue
|
||||
}
|
||||
fmt.Println(index)
|
||||
}
|
||||
//break打印出来10、9、8、7、6
|
||||
//continue打印出来10、9、8、7、6、4、3、2、1
|
||||
|
||||
|
||||
for可以用于读取slice和map的数据,配合range
|
||||
|
||||
for k,v:=range map {
|
||||
fmt.Println("map's key:",k)
|
||||
fmt.Println("map's val:",v)
|
||||
}
|
||||
|
||||
其中还可以使用`_`来扔掉不需要的返回值
|
||||
|
||||
|
||||
###switch
|
||||
有些时候你需要写很多的`if/else`来实现一些逻辑处理,这个时候代码看上去就很丑很冗长,而且也不易于以后的维护,这个时候switch就能很好的解决这个问题,他的语法如下
|
||||
|
||||
switch sExpr {
|
||||
case expr1:
|
||||
some instructions
|
||||
case expr2:
|
||||
some other instructions
|
||||
case expr3:
|
||||
some other instructions
|
||||
default:
|
||||
other code
|
||||
}
|
||||
|
||||
sExpr和expr1、expr2、expr3的类型必须一致。Go的switch非常灵活。表达式不必是常量或整数,执行的过程从上至下,直到找到匹配项,而如果switch没有表达式,它会匹配true。
|
||||
|
||||
i := 10
|
||||
switch i {
|
||||
case 1:
|
||||
fmt.Println("i is equal to 1")
|
||||
case 2, 3, 4:
|
||||
fmt.Println("i is equal to 2, 3 or 4")
|
||||
case 10:
|
||||
fmt.Println("i is equal to 10")
|
||||
default:
|
||||
fmt.Println("All I know is that i is an integer")
|
||||
}
|
||||
|
||||
我们看第六行,我们把很多值聚合在了一个case里面,同时,Go里面switch默认相当于每个case后面带有break,匹配成功后不会自动向下尝试,而是跳出整个switch了,但是可以使用fallthrough使其这样做。
|
||||
|
||||
integer := 6
|
||||
switch integer {
|
||||
case 4:
|
||||
fmt.Println("The integer was <= 4")
|
||||
fallthrough
|
||||
case 5:
|
||||
fmt.Println("The integer was <= 5")
|
||||
fallthrough
|
||||
case 6:
|
||||
fmt.Println("The integer was <= 6")
|
||||
fallthrough
|
||||
case 7:
|
||||
fmt.Println("The integer was <= 7")
|
||||
fallthrough
|
||||
case 8:
|
||||
fmt.Println("The integer was <= 8")
|
||||
fallthrough
|
||||
default:
|
||||
fmt.Println("default case")
|
||||
}
|
||||
|
||||
上面的程序讲输出
|
||||
|
||||
The integer was <= 6
|
||||
The integer was <= 7
|
||||
The integer was <= 8
|
||||
default case
|
||||
|
||||
|
||||
##函数
|
||||
函数是Go里面的核心设计,Go的函数申明通过关键字`func`来申明,他的格式如下
|
||||
|
||||
func funcname(q int) (r,s int) { return 0,0 }
|
||||
|
||||
###多个返回值
|
||||
###变参
|
||||
###defer
|
||||
###递归函数
|
||||
Reference in New Issue
Block a user