一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

腳本之家,腳本語言編程技術及教程分享平臺!
分類導航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務器之家 - 腳本之家 - Golang - Go語言中的if條件語句使用詳解

Go語言中的if條件語句使用詳解

2020-04-28 10:30腳本之家 Golang

這篇文章主要介紹了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,那么語句之后第一行代碼會被執行。

 

流程圖:

Go語言中的if條件語句使用詳解

例子:

復制代碼 代碼如下:


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


讓我們編譯和運行上面的程序,這將產生以下結果:

 

?
1
2
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代碼塊將被執行。

 

流程圖:

Go語言中的if條件語句使用詳解

例子:

復制代碼 代碼如下:


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);

}


當上述代碼被編譯和執行時,它產生了以下結果:

 

?
1
2
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 )
}


讓我們編譯和運行上面的程序,這將產生以下結果:

 

?
1
2
None of the values is matching
Exact value of a is: 100

 

延伸 · 閱讀

精彩推薦
  • Golanggolang的httpserver優雅重啟方法詳解

    golang的httpserver優雅重啟方法詳解

    這篇文章主要給大家介紹了關于golang的httpserver優雅重啟的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,...

    helight2992020-05-14
  • GolangGolang中Bit數組的實現方式

    Golang中Bit數組的實現方式

    這篇文章主要介紹了Golang中Bit數組的實現方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧...

    天易獨尊11682021-06-09
  • Golanggo日志系統logrus顯示文件和行號的操作

    go日志系統logrus顯示文件和行號的操作

    這篇文章主要介紹了go日志系統logrus顯示文件和行號的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧...

    SmallQinYan12302021-02-02
  • Golanggolang如何使用struct的tag屬性的詳細介紹

    golang如何使用struct的tag屬性的詳細介紹

    這篇文章主要介紹了golang如何使用struct的tag屬性的詳細介紹,從例子說起,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看...

    Go語言中文網11352020-05-21
  • Golanggolang 通過ssh代理連接mysql的操作

    golang 通過ssh代理連接mysql的操作

    這篇文章主要介紹了golang 通過ssh代理連接mysql的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧...

    a165861639710342021-03-08
  • Golanggolang json.Marshal 特殊html字符被轉義的解決方法

    golang json.Marshal 特殊html字符被轉義的解決方法

    今天小編就為大家分享一篇golang json.Marshal 特殊html字符被轉義的解決方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧 ...

    李浩的life12792020-05-27
  • GolangGolang通脈之數據類型詳情

    Golang通脈之數據類型詳情

    這篇文章主要介紹了Golang通脈之數據類型,在編程語言中標識符就是定義的具有某種意義的詞,比如變量名、常量名、函數名等等,Go語言中標識符允許由...

    4272021-11-24
  • Golanggo語言制作端口掃描器

    go語言制作端口掃描器

    本文給大家分享的是使用go語言編寫的TCP端口掃描器,可以選擇IP范圍,掃描的端口,以及多線程,有需要的小伙伴可以參考下。 ...

    腳本之家3642020-04-25
主站蜘蛛池模板: 日本午夜vr影院新入口 | 日本免费高清在线观看播放 | 日本乱中文字幕系列在线观看 | 国产精品29页 | 99久久久久国产 | 8x8x极品国产在线 | 天堂素人在线 | 性色香蕉AV久久久天天网 | 亚洲区精品| 成 人 免费 小说在线观看 | 校花被扒开尿口折磨憋尿 | 亚洲欧美日韩成人一区在线 | 九九热在线视频 | 视频在线91 | 波多野结衣女教师在线观看 | 亚洲性久久久影院 | 国产精品久久99 | 国产一区二区三区久久精品小说 | 午夜DV内射一区区 | 亚洲成人影院在线观看 | 色哟哟哟在线精品观看视频 | 99精品观看 | 国产成人愉拍免费视频 | 国产亚洲人成网站在线观看不卡 | 四虎黄色影视 | 全日本爽视频在线 | 情侣奴伺候女王第2部分小说 | 四虎最新永久免费网址 | 日麻逼| 百合女女师生play黄肉黄 | 欧美香蕉| 亚洲国产99在线精品一区69堂 | 日韩成人精品在线 | 法国贵妇一级伦理hd | 国内精品久久久久久久久久久久 | 日本剧情片在线播放中文版 | 99久久免费看精品国产一区 | 欧美专区在线观看 | 国产情侣偷国语对白 | 青青国产在线观看 | 色哟哟国产成人精品 |