条件语句
一:if...else...
1 package main 2 3 import "fmt" 4 5 func main() { 6 var num = 10 7 var num2 = 20 8 res := test(num, num2) 9 fmt.Println(res) 10 } 11 12 func test(num, num2 int) int { 13 var result int 14 if num > num2 { 15 result = num 16 } else { 17 result = num2 18 } 19 return result 20 }
二:switch...case...
1 package main 2 3 import ( 4 "fmt" 5 ) 6 7 func main() { 8 var num = 999 9 var num2 = 99 10 res := test2(num, num2) 11 fmt.Println(res) 12 } 13 14 func test2(num, num2 int) int { 15 var result int 16 switch num > num2 { 17 case true: 18 result = num 19 case false: 20 result = num2 21 } 22 return result 23 }
不知道写的规范不,本地能跑起来