sunwenfei
  • 关于我
  • Golang
    • Goglang基础教程【译】
      • 介绍
        • 安装
        • Hello World
      • 变量、基本类型以及常量
        • 变量
        • 基本类型
        • 常量
      • 函数和包
        • 函数
        • 包
      • 条件、循环流程控制语句
        • if else条件语句
        • switch语句
        • 循环语句
      • 数组、切片、变参函数
        • 数组(Array)
        • 切片(Slice)
        • 变参函数
      • 其他数据类型
        • 映射(Map)
        • 字符串
      • 指针、结构体和方法
        • 指针
        • 结构体
        • 方法
      • 面向对象编程
        • 结构体 vs 类
        • 组合 vs 继承
        • 接口
        • 多态
      • 并发
        • 并发介绍
        • 协程(goroutine)
        • 管道(channel)
        • 带缓存的管道(buffered channel)
        • 协程池
        • 管道选择器(select)
        • 互斥锁(Mutex)
      • Defer
      • 一等公民函数
      • 反射
      • 错误
        • 错误处理
        • 自定义错误类型
        • panic和recover
      • 文件读写
        • 读文件
        • 写文件
    • Golang面向对象编程
    • Golang函数式编程
    • Golang并发编程
    • Golang web服务编程
    • Golang数据结构与算法
  • Shell编程
    • Find命令
  • JavaScript
    • browser
    • Node.JS
    • Deno
  • TypeScript
  • HTTP
    • 【译】通过信鸽理解HTTPS交互原理
  • React
    • React16
      • Hooks
        • 使用React Hooks拉取数据
  • 移动端开发
    • 原生
    • Flutter
    • ReactNative
    • 小程序
  • 前端测试
Powered by GitBook
On this page

Was this helpful?

  1. Golang
  2. Goglang基础教程【译】
  3. 条件、循环流程控制语句

if else条件语句

本节介绍Golang中的条件语句if else。if语句的语法如下:

if condition {

}

condition条件满足时执行大括号中的代码块。Golang比JavaScript等语言更严格,即使大括号中仅有一条语句,大括号也不能省略。

跟大多数编程语言类似,Golangif条件语句也支持else if和else:

if condition {

} else if condition {

} else {

}

当然,else if语句可以存在多个。

当if和else if任何一个条件判断通过时,执行对应的代码块得,否则就说明所有这些判断条件都不通过,那么就执行else对应的代码块。

我们写一个程序来判断一个数字是奇数还是偶数。

package main

import "fmt"

func main() {
    num := 10
    if num % 2 == 0 {
        fmt.Println("the number is even")
    } else {
        fmt.Println("the number is odd")
    }
}

Golang中的if有个变体,可以在判断条件condition之前加一个语句,语法如下:

if statement; condition {

}

我们用这种写法重构下前面的程序。

package main

import "fmt"

func main() {
    if num:= 10; num % 2 == 0 {
        fmt.Println("the number is even")
    } else {
        fmt.Println("the number is odd")
    }
}

我们现在写一个程序来体验下else if的用法。

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")
    }
}

防坑指南

Golang中条件语句的else不能另起一行,必须跟在}符号后面,否则编译会报错。

package main

import "fmt"

func main() {  
    num := 10
    if num % 2 == 0 {
        fmt.Println("the number is even") 
    }  
    else {
        fmt.Println("the number is odd")
    }
}
syntax error: unexpected else, expecting }

这时我们的代码会变成:

if num % 2 == 0 {  
      fmt.Println("the number is even") 
};  //semicolon inserted by Go
else {  
      fmt.Println("the number is odd")
}

由于if else一个完整的语句,在中间插入了;当然报错了。起初可能不太习惯,不过换个角度看,这一点反倒使得Golang的语法更加严格,要求大家写的代码也更加一致。

Previous条件、循环流程控制语句Nextswitch语句

Last updated 6 years ago

Was this helpful?

这里的if num % 2 == 0语句判断num对2求模的余数是否是0。如果是,那么就打印"the number is even",否则就打印"the number is odd"。由于这里num赋值为10,是偶数,因此这段程序会打印the number is even。

这种写法的不同点在于,num变量初始化是在if语句内完成的,因此num变量的作用域也仅限与if语句(当然也包括else)内,因此也就无法在该if else语句外访问num变量。

这里由于num初始化赋值为99,因此num >= 51 && num <= 100条件判断通过,因此打印输出number is between 51 and 100。

这段代码里else子句没有紧跟在}符号后面,而是另起新的一行,因此会报错:

为什么会这样呢?原因是因为Golang会在位于行尾的}符号后面自动补一个分号,具体可以参考这里:。

Go Playground在线运行
Go Playground在线运行
Go Playground在线运行
Go Playground在线运行
https://golang.org/ref/spec#Semicolons