学习 Golang 教程 - if else 语句示例

125 阅读2分钟

在这篇博文中,我们将通过实例学习Go语言中的控制流结构--if-else语句。

golang的if语句

像许多编程语言一样,Go语言提供了用于决策的控制结构。if和else语句是其中一种类型。if语句用于根据条件表达式执行一段代码,如果条件得到满足,代码块将被执行。

if 和else在golang中是保留字。

这篇博文涵盖了以下内容的例子和语法

  • 简单的if语句
  • if速记语句
  • if else 语句
  • if else嵌套语句

以下是Golang中if语句的语法

if(Conditional Expression) {  
Code Block  
}  

条件表达式被评估,结果总是truefalse

这个表达式可以是简单的布尔值,也可以是使用比较运算符创建的复杂表达式==,=!=

表达式可以是singlemultiple

多个表达式用逻辑运算符连接,如&&,||,!运算符。条件表达式不需要用小括号括起来。

但是代码块必须用大括号括起来。在Golang中,包含单语句的代码块需要使用大括号。

Golang中简单的if语句

这个例子评估了表达式,如果表达式为真,代码块被执行,并在控制台返回 "事件编号测试通过"。下面是一个简单的if条件表达式的例子。

package main  
  
import "fmt"  
  
func main() {  
	var numerVariable = 10  
	if numerVariable%2 == 0 {  
		fmt.Printf("Event Number test passed ")  
	}  
}  

以上代码的输出:

Event Number test passed 

让我们看看if else代码块中的一个错误案例。

下面的示例代码给出了一个编译错误,原因是如果语句代码块没有大括号,它会抛出 语法错误:unexpected newline, expecting { after if clause.

package main  
  
import "fmt"  
  
func main() {  
	var numerVariable = 10  
	if numerVariable %2 == 0   
		fmt.Printf("Event Number test passed ")  
}  

多个表达式可以与逻辑运算符相结合

var number = 2  
if  number >= 1 && age <= 10 {  
	fmt.Println("Number is between 1 to 10")  
} 

Golang中if与速记语句的语法

在这里,if语句总是包含shorthand statements ,然后才进入条件表达式的控制。

shorthand statements 可以包含变量声明,并且变量的范围只限于本块的范围内。

func main() {  
	var numerVariable = 10  
	if result := numerVariable % 2; result == 0 {  
		fmt.Printf("Event Number test passed ")  
	}  
}  

如果你在if语句中包含shorthand syntax ,那么小括号是不需要的。

它会产生compilation error - syntax error: unexpected:=, expecting ),如果小括号是速记语法的话。

func main() {  
	var numerVariable = 10  
	if (result := numerVariable % 2; result == 0) {  
		fmt.Printf("Event Number test passed ")  
	}  
}  

Golang中的if else语句

这种语法包含if语句的else块。如果条件表达式为真,则执行该块,否则执行else块。

if (conditional expression){  
//if true this block of code is executed  
}else{  
//if false block of code is executed  
}  

if else 语句的例子

package main  
  
import "fmt"  
  
func main() {  
	var numerVariable = 10  
	if numerVariable%2 == 0 {  
		fmt.Printf("Event Number if block ")  
	} else {  
		fmt.Printf("Odd Number else block")  
  
	}  
}  

上述程序的输出是

Event Number if block  

多个if else - Golang中的嵌套if语句

如果语句中包含if else语句,包含if else语句的链子

package main  
  
import "fmt"  
  
func main() {  
	var numerVariable = 1001  
	if numerVariable <= 100 {  
		fmt.Printf("Number is less than 100")  
	} else if numerVariable > 100 && numerVariable <= 1000 {  
		fmt.Printf("Number is greater than 100 and less than 1000")  
	} else { // else statements  
		fmt.Printf("Number is greater than 1000")  
	}  
}  

上述程序的输出结果是

Number is greater than 1000  

如果在else语句前有任何断行,就像下面的例子一样,编译错误,下面的程序会出现syntax error: unexpected else, expecting }

func main() {  
	var numerVariable = 1001  
	if numerVariable <= 100 {  
		fmt.Printf("Number is less than 100")  
	} else if numerVariable > 100 && numerVariable <= 1000 {  
		fmt.Printf("Number is greater than 100 and less than 1000")  
    }   
    else { // else statements  
		fmt.Printf("Number is greater than 1000")  
	}  
}  

总结

在本教程中,你学习了Golang中的条件流if else块的例子。