Go语言中的if条件语句使用详解

 更新时间:2015年10月29日 16:05:28   投稿:goldensun  
这篇文章主要介绍了Go语言中的if条件语句的使用,包括if...else语句以及相关嵌套,需要的朋友可以参考下

if语句
if语句包含一个布尔表达式后跟一个或多个语句。

语法
if语句在Go编程语言的语法是:

复制代码 代码如下:

if(boolean_expression)
{
   /* statement(s) will execute if the boolean expression is true */
}

如果布尔表达式的值为 true,那么if语句里面代码块将被执行。如果if语句的结束(右大括号后)布尔表达式的值为false,那么语句之后第一行代码会被执行。

流程图:

20151029155623767.jpg (254×321)

例子:

复制代码 代码如下:

package main

import "fmt"

func main() {
   /* local variable definition */
   var a int = 10
 
   /* check the boolean condition using if statement */
   if( a < 20 ) {
       /* if condition is true then print the following */
       fmt.Printf("a is less than 20\n" )
   }
   fmt.Printf("value of a is : %d\n", a)
}


让我们编译和运行上面的程序,这将产生以下结果:

a is less than 20;
value of a is : 10


if...else语句
if语句可以跟着一个可选的else语句,布尔表达式是假时它被执行。

语法
在Go编程语言中的if ... else语句的语法是:

复制代码 代码如下:

if(boolean_expression)
{
   /* statement(s) will execute if the boolean expression is true */
}
else
{
  /* statement(s) will execute if the boolean expression is false */
}

如果布尔表达式的值为true,那么if代码块将被执行,否则else代码块将被执行。

流程图:

20151029160444006.jpg (251×321)

例子:

复制代码 代码如下:

package main

import "fmt"

func main() {
   /* local variable definition */
   var a int = 100;
 
   /* check the boolean condition */
   if( a < 20 ) {
       /* if condition is true then print the following */
       fmt.Printf("a is less than 20\n" );
   } else {
       /* if condition is false then print the following */
       fmt.Printf("a is not less than 20\n" );
   }
   fmt.Printf("value of a is : %d\n", a);

}


当上述代码被编译和执行时,它产生了以下结果:

a is not less than 20;
value of a is : 100

 if...else if...else 语句
if语句可以跟着一个可选的else if ... else语句,这是非常有用的使用单个 if...else if 语句声明测试各种条件。

当使用if , else if , else语句有几点要记住使用:

if可以有零或一个else,它必须跟从else if后面。

一个if可以有零到个多else if并且它们必须在else之前。

一旦一个else if测试成功,其它任何剩余else if将不会被测试。

语法
if...else if...else在Go编程语言中语句的语法是:

复制代码 代码如下:

if(boolean_expression 1)
{
   /* Executes when the boolean expression 1 is true */
}
else if( boolean_expression 2)
{
   /* Executes when the boolean expression 2 is true */
}
else if( boolean_expression 3)
{
   /* Executes when the boolean expression 3 is true */
}
else
{
   /* executes when the none of the above condition is true */
}

例子:
复制代码 代码如下:

package main

import "fmt"

func main() {
   /* local variable definition */
   var a int = 100
 
   /* check the boolean condition */
   if( a == 10 ) {
       /* if condition is true then print the following */
       fmt.Printf("Value of a is 10\n" )
   } else if( a == 20 ) {
       /* if else if condition is true */
       fmt.Printf("Value of a is 20\n" )
   } else if( a == 30 ) {
       /* if else if condition is true  */
       fmt.Printf("Value of a is 30\n" )
   } else {
       /* if none of the conditions is true */
       fmt.Printf("None of the values is matching\n" )
   }
   fmt.Printf("Exact value of a is: %d\n", a )
}


让我们编译和运行上面的程序,这将产生以下结果:

None of the values is matching
Exact value of a is: 100

相关文章

  • golang中tar压缩和解压文件详情

    golang中tar压缩和解压文件详情

    这篇文章主要给大家介绍golang中tar压缩和解压文件,文章以查看官方文档自带的给大家演习一下golang的archive/tar压缩和解压功能,需要的朋友可以参考一下
    2021-11-11
  • Golang实现CronJob(定时任务)的方法详解

    Golang实现CronJob(定时任务)的方法详解

    这篇文章主要为大家详细介绍了Golang如何通过一个单 pod 去实现一个常驻服务,去跑定时任务(CronJob),文中的示例代码讲解详细,需要的可以参考下
    2023-04-04
  • 浅析Go语言中的超时控制

    浅析Go语言中的超时控制

    日常开发中我们大概率会遇到超时控制的场景,而一个良好的超时控制可以有效的避免一些问题,所以本文就来和大家深入探讨一下Go语言中的超时控制吧
    2023-10-10
  • goland 清除所有的默认设置操作

    goland 清除所有的默认设置操作

    这篇文章主要介绍了goland 清除所有的默认设置操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-04-04
  • golang中定时器cpu使用率高的现象详析

    golang中定时器cpu使用率高的现象详析

    这篇文章主要给大家介绍了关于golang中定时器cpu使用率高的现象的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
    2018-04-04
  • golang中数组与切片的区别详析

    golang中数组与切片的区别详析

    数组是固定长度,常量,切片长度是可以改变,所以是一个可变的数组,下面这篇文章主要给大家介绍了关于golang中数组与切片区别的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2022-11-11
  • 解析GOROOT、GOPATH、Go-Modules-三者的关系

    解析GOROOT、GOPATH、Go-Modules-三者的关系

    这篇文章主要介绍了解析GOROOT、GOPATH、Go-Modules-三者的关系,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-10-10
  • GO将mysql 中 decimal 数据类型映射到 protobuf的操作方法

    GO将mysql 中 decimal 数据类型映射到 protobuf的操作方法

    这篇文章主要介绍了go如何优雅地将 mysql 中 decimal 数据类型映射到 protobuf,本文主要展示一下在 protobuf中 float与double的一个区别,结合实例代码给大家介绍的非常详细,需要的朋友可以参考下
    2022-09-09
  • golang int 转float 强转和高精度转操作

    golang int 转float 强转和高精度转操作

    这篇文章主要介绍了golang int 转float 强转和高精度转操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-12-12
  • Go routine调度详解

    Go routine调度详解

    这篇文章主要介绍了Go routine调度详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-01-01

最新评论