Go 语言笔记:条件语句
条件语句
Go 语言中的条件语句,就是我们在其他语言中常见的 if 和 else。
单分支
go
score := 95
if score >= 90 {
fmt.Println("优秀")
}双分支
go
score := 85
if score >= 90 {
fmt.Println("优秀")
} else {
fmt.Println("继续努力")
}多分支
go
score := 85
if score >= 90 {
fmt.Println("优秀")
} else if score >= 75 {
fmt.Println("良好")
} else if score >= 60 {
fmt.Println("一般")
} else {
fmt.Println("差")
}高级写法
statement 是可选部分,在 condition 之前执行
go
if statement; condition {
}注意事项
Go 语言中,对于条件语句的闭合是有严格要求的
- 不管分支内有几条语句,都必须以
{}闭合,不能省略 else和else if两边的{}必须在同一行