• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >Golang > Go语言中的if条件语句使用详解

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

作者:goldensun 字体:[增加 减小] 来源:互联网

goldensun 通过本文主要向大家介绍了go语言,go语言环境搭建,go语言程序设计,let it go 25种语言,go语言教程等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

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

语法
if语句在Go编程语言的语法是:
if(boolean_expression)
{
   /* statement(s) will execute if the boolean expression is true */
}
</div>
如果布尔表达式的值为 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)
}
</div>
让我们编译和运行上面的程序,这将产生以下结果:

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

</div>


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 */
}
</div>
如果布尔表达式的值为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);

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

a is not less than 20;
value of a is : 100
</div>

 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 */
}
</div>
例子:
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 )
}
</div>
让我们编译和运行上面的程序,这将产生以下结果:

None of the values is matching
Exact value of a is: 100
</div>

</div>

您可能想查找下面的文章:

  • 利用Go语言搭建WebSocket服务端方法示例
  • Go语言字典(map)用法实例分析【创建,填充,遍历,查找,修改,删除】
  • Go语言中更优雅的错误处理
  • Go语言实现的排列组合问题实例(n个数中取m个)
  • Go语言Cookie用法分析
  • GO语言运行环境下载、安装、配置图文教程
  • go语言文件正则表达式搜索功能示例
  • Go语言正则表达式用法实例小结【查找、匹配、替换等】
  • Go语言正则表达式示例
  • Go语言中三种不同md5计算方式的性能比较

相关文章

  • 利用Go语言实现简单Ping过程的方法
  • GO语言(golang)基础知识
  • go语言实现文件分割的方法
  • Go语言里的结构体文法实例分析
  • Go语言字典(map)用法实例分析【创建,填充,遍历,查找,修改,删除】
  • 深入剖析Go语言编程中switch语句的使用
  • Golang使用zlib压缩和解压缩字符串
  • Go语言多值替换的HTML模板实例分析
  • Go语言实现的最简单数独解法
  • 举例讲解Go语言中函数的闭包使用

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • GO语言延迟函数defer用法分析
    • Go语言map用法实例分析
    • Go语言的GOPATH与工作目录详解
    • Golang极简入门教程(一):基本概念
    • go语言if/else语句简单用法示例
    • go语言通过反射获取和设置结构体字段值的方法
    • Go语言常用字符串处理方法实例汇总
    • GO语言的IO方法实例小结
    • go语言通过管道连接两个命令行进程的方法
    • Go语言中常量定义方法实例分析

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有