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

相关文章

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

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

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

    golang如何通过type定义函数类型

    这篇文章主要介绍了golang如何通过type定义函数类型问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-01-01
  • Golang使用Gin实现文件上传的示例代码

    Golang使用Gin实现文件上传的示例代码

    本文我们主要介绍了Golang如何使用Gin实现文件上传,Go标准库net/http对文件上传已经提供了非常完善的支持,而Gin框架在其基础上进一步封装,因此使用Gin开发文件上传功能时,只需要简单几行代码便可以实现,需要的朋友可以参考下
    2024-02-02
  • go语言LeetCode题解1030距离顺序排列矩阵单元格

    go语言LeetCode题解1030距离顺序排列矩阵单元格

    这篇文章主要为大家介绍了go语言LeetCode题解1030距离顺序排列矩阵单元格,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-12-12
  • Golang之sync.Pool对象池对象重用机制总结

    Golang之sync.Pool对象池对象重用机制总结

    这篇文章主要对Golang的sync.Pool对象池对象重用机制做了一个总结,文中有相关的代码示例和图解,具有一定的参考价值,需要的朋友可以参考下
    2023-07-07
  • go-zero服务部署配置及源码解读

    go-zero服务部署配置及源码解读

    这篇文章主要为大家介绍了go-zero服务部署配置及源码解读,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-08-08
  • golang常用库之pkg/errors包第三方错误处理包案例详解

    golang常用库之pkg/errors包第三方错误处理包案例详解

    这篇文章主要介绍了golang常用库之pkg/errors包第三方错误处理包,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-03-03
  • go defer避坑指南之拆解延迟语句

    go defer避坑指南之拆解延迟语句

    这篇文章主要为大家详细介绍了go defer避坑指南之如何拆解延迟语句,掌握正确使用方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
    2023-11-11
  • go mod tidy命令的使用

    go mod tidy命令的使用

    gomodtidy命令是Go语言中用于管理项目依赖的工具,主要功能包括移除未使用的依赖项、添加缺失的依赖项以及更新go.sum文件以确保依赖项的正确校验,感兴趣的可以了解一下
    2024-11-11
  • Go语言排序算法:快速、可靠的排序解决方案

    Go语言排序算法:快速、可靠的排序解决方案

    Go语言提供了多种快速、可靠的排序算法,可以满足不同场景下的排序需求,其中最常用的排序算法包括快速排序、归并排序和堆排序,需要的朋友可以参考下
    2023-10-10

最新评论