go 流程控制
Posted ctztake
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了go 流程控制相关的知识,希望对你有一定的参考价值。
if else 语句
基本语法
if condition { //do something } if condition { //do something } else if condition { //do something } else {
}
if statement; condition {
//do something
}
练习一
package main import ( "fmt" ) func main() { num := 10 if num % 2 == 0 { //checks if number is even fmt.Println("the number is even") } else { fmt.Println("the number is odd") } }
练习二
package main import ( "fmt" ) func main() { if num := 10; num % 2 == 0 { //checks if number is even fmt.Println(num,"is even") } else { fmt.Println(num,"is odd") } }
练习三
package main import ( "fmt" ) func main() { num := 99 if num <= 50 { fmt.Println("number is less than or equal to 50") } else if num >= 51 && num <= 100 { fmt.Println("number is between 51 and 100") } else { fmt.Println("number is greater than 100") } }
循坏
Go语言中只有一种循环 for
for initialisation; condition; post { }
练习一
package main import ( "fmt" ) func main() { for i := 1; i <= 10; i++ { fmt.Printf(" %d",i) } }
break,终止循环
package main import ( "fmt" ) func main() { for i := 1; i <= 10; i++ { if i > 5 { break //loop is terminated if i > 5 } fmt.Printf("%d ", i) } fmt.Printf(" line after for loop") }
continue 终止本次循坏
package main import ( "fmt" ) func main() { for i := 1; i <= 10; i++ { if i%2 == 0 { continue } fmt.Printf("%d ", i) } }
省略写法
package main import ( "fmt" ) func main() { i := 0 for ;i <= 10; { // initialisation and post are omitted fmt.Printf("%d ", i) i += 2 } }
package main import ( "fmt" ) func main() { i := 0 for i <= 10 { // initialisation and post are omitted fmt.Printf("%d ", i) i += 2 } }
练习
package main import ( "fmt" ) func main() { for no, i := 10, 1; i <= 10 && no <= 19; i, no = i+1, no+1 { fmt.Printf("%d * %d = %d ", no, i, no*i) } }
无限循环
package main import ( "fmt" ) func main() { for { fmt.Printf("hello") } }
switch 语句
switch
package main import ( "fmt" ) func main() { finger := 4 switch finger { case 1: fmt.Println("Thumb") case 2: fmt.Println("Index") case 3: fmt.Println("Middle") case 4: fmt.Println("Ring") case 5: fmt.Println("Pinky") } }
Switch default
package main import ( "fmt" ) func main() { switch finger := 8; finger { case 1: fmt.Println("Thumb") case 2: fmt.Println("Index") case 3: fmt.Println("Middle") case 4: fmt.Println("Ring") case 5: fmt.Println("Pinky") default: //default case fmt.Println("incorrect finger number") } }
case多个值
package main import ( "fmt" ) func main() { letter := "i" switch letter { case "a", "e", "i", "o", "u": fmt.Println("vowel") default: fmt.Println("not a vowel") } }
Switch case 条件判断
package main import ( "fmt" ) func main() { num := 75 switch { // expression is omitted case num >= 0 && num <= 50: fmt.Println("num is greater than 0 and less than 50") case num >= 51 && num <= 100: fmt.Println("num is greater than 51 and less than 100") case num >= 101: fmt.Println("num is greater than 100") } }
Switch fallthrough
带有 fallthrough 的case语句执行玩本条语句后继续向下执行
package main import ( "fmt" ) func number() int { num := 15 * 5 return num } func main() { switch num := number(); { //num is not a constant case num < 50: fmt.Printf("%d is lesser than 50 ", num) fallthrough case num < 100: fmt.Printf("%d is lesser than 100 ", num) fallthrough case num < 200: fmt.Printf("%d is lesser than 200", num) } }
以上是关于go 流程控制的主要内容,如果未能解决你的问题,请参考以下文章