Golang关键字—— if/else
Golang中,if/else 关键字用于条件判断,如果满足条件就做某事,否则做另一件事:
if age >= 18 { fmt.Println("成年人") } else { fmt.Println("未成年") }
多重判断:
if score >= 90 { fmt.Println("优秀") } else if score >= 70 { fmt.Println("良好") } else if score >= 60 { fmt.Println("一般") } else { fmt.Println("差") }
Golang允许在条件判断语句里声明一个变量,该变量的作用域只在该条件逻辑块内:
if score := 40; score >= 90 { fmt.Println("优秀:", score) } else if score >= 70 { fmt.Println("良好") } else if score >= 60 { fmt.Println("一般") } else { fmt.Println("差:", score) }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步