我是靠谱客的博主 曾经棉花糖,这篇文章主要介绍go 流程语句 if goto for swich,现在分享给大家,希望可以做个参考。

流程控制

流程控制在编程语言中是最伟大的发明了,因为有了它,你可以通过很简单的流程描述来表达很复杂的逻辑。Go中流程控制分三大类:条件判断,循环控制和无条件跳转。

if

if也许是各种编程语言中最常见的了,它的语法概括起来就是:如果满足条件就做某事,否则做另一件事。

Go里面if条件判断语句中不需要括号,如下代码所示

复制代码
1
2
3
4
5
if x > 10 { fmt.Println("x is greater than 10") } else { fmt.Println("x is less than 10") }

  Go的if还有一个强大的地方就是条件判断语句里面允许声明一个变量,

这个变量的作用域只能在该条件逻辑块内,其他地方就不起作用了,如下所示

复制代码
1
2
3
4
5
6
7
8
9
// 计算获取值x,然后根据x返回的大小,判断是否大于10。 if x := computedValue(); x > 10 { fmt.Println("x is greater than 10") } else { fmt.Println("x is less than 10") } //这个地方如果这样调用就编译出错了,因为x是条件里面的变量 fmt.Println(x)

  

多个条件的时候如下所示:

复制代码
1
2
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跳转到必须在当前函数内定义的标签。例如假设这样一个循环:

复制代码
1
2
3
4
5
6
7
8
unc myFunc() { i := 0 Here: //这行的第一个词,以冒号结束作为标签 println(i) i++ goto Here //跳转到Here去 } 标签名是大小写敏感的。

 for

Go里面最强大的一个控制逻辑就是for,它既可以用来循环读取数据,

又可以当作while来控制逻辑,还能迭代操作。它的语法如下:

复制代码
1
 
复制代码
1
2
3
for expression1; expression2; expression3 { //... }

 

xpression1expression2expression3都是表达式,其中expression1expression3是变量声明或者函数调用返回值之类的,expression2是用来条件判断,expression1在循环开始之前调用,expression3在每轮循环结束之时调用。

一个例子比上面讲那么多更有用,那么我们看看下面的例子吧:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
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

有些时候如果我们忽略expression1expression3

复制代码
1
2
3
4
sum := 1 for ; sum < 1000; { sum += sum }

  其中;也可以省略,那么就变成如下的代码了,是不是似曾相识?对,这就是while的功能。

复制代码
1
2
3
4
sum := 1 for sum < 1000 { sum += sum }

  在循环里面有两个关键操作breakcontinue ,break操作是跳出当前循环

continue是跳过本次循环。当嵌套过深的时候,break可以配合标签使用,即跳转至标签所指定的位置,详细参考如下例子:

 

复制代码
1
2
3
4
5
6
7
8
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

  

breakcontinue还可以跟着标号,用来跳到多重循环中的外层循环

for配合range可以用于读取slicemap的数据:

复制代码
1
2
for k,v:=range map { fmt.Println("map's key:",k) fmt.Println("map's val:",v) }

由于 Go 支持 “多值返回”, 而对于“声明而未被调用”的变量, 编译器会报错, 在这种情况下, 可以使用_来丢弃不需要的返回值 例如

复制代码
1
for _, v := range map{ fmt.Println("map's val:", v) }

 

witch

有些时候你需要写很多的if-else来实现一些逻辑处理,

这个时候代码看上去就很丑很冗长,而且也不易于以后的维护,

这个时候switch就能很好的解决这个问题。它的语法如下

 

复制代码
1
2
3
4
5
6
7
8
9
10
switch sExpr { case expr1: some instructions case expr2: some other instructions case expr3: some other instructions default: other code }

  sExprexpr1expr2expr3的类型必须一致。Go的switch非常灵活,

表达式不必是常量或整数,执行的过程从上至下,直到找到匹配项;而如果switch没有表达式,它会匹配true

 

复制代码
1
2
3
4
5
6
7
8
9
10
11
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") }

  在第5行中,我们把很多值聚合在了一个case里面,同时,

Go里面switch默认相当于每个case最后带有break

匹配成功后不会自动向下执行其他case,

而是跳出整个switch, 但是可以使用fallthrough强制执行后面的case代码。

 

 

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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") }

  

 

 

 

 

 

 

 

 

 

复制代码
1



转载于:https://www.cnblogs.com/lnrick/p/9273282.html

最后

以上就是曾经棉花糖最近收集整理的关于go 流程语句 if goto for swich的全部内容,更多相关go内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(55)

评论列表共有 0 条评论

立即
投稿
返回
顶部