golang的条件语句:
package main import ( "fmt" ) func main(){ age := 18 // if else if else 语句 if age == 18 { fmt.Println("your age is 18") }else if age > 18{ fmt.Println("your age is bigger than 18") }else { fmt.Println("your age is smaller than 18") } //swith 语句 匹配一个就退出 switch age { case 16: fmt.Println(your age is 16) case 18: fmt.Println(your age is 18) default: fmt.Println(your age is not 16 or 18) } //switch语句case可以包含多个元素,逗号隔开 switch age { case 16,18,20,30: fmt.Println(xxx) default: fmt.Println(your age is not 16 or 18) } //switch语句另一种写法 switch { case age < 18: fmt.Println(your age is smaller than 18) case age > 18: fmt.Println(your age is bigger than 18) default: fmt.Println(your age is not 16 or 18) } // fallthrough,穿透下一个条件执行语句 switch { case age < 18: fmt.Println(your age is smaller than 18) fallthrough case age > 18: fmt.Println(your age is bigger than 18) default: fmt.Println(your age is not 16 or 18) } 执行结果: your age is smaller than 18 your age is bigger than 18 // for 循环 for i :=0;i <10;i++{ fmt.Println(i) } // for 循环另一种写法 i := 0 for ; i<10;i++{ fmt.Println(i) } //for 循环另一种写法 i := 0 for i < 10{ fmt.Println(i) i++ } // for 死循环 for { fmt.Println("1") } }