激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

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

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
主站蜘蛛池模板: 久久性生活免费视频 | 青草伊人网 | 免费一级欧美在线观看视频 | 182tv成人福利视频免费看 | 久久精品无码一区二区日韩av | 亚洲欧美日韩综合一区 | 亚洲白嫩在线观看 | 久久久tv| 一区二区三区日韩 | 国产精品免费一区二区三区都可以 | 国产伦精品一区二区三区 | 午夜色片| 韩国三级日本三级香港三级黄 | 国产黄色网页 | 视频一区国产精品 | 午夜精品福利视频 | 久久国产精品小视频 | 国产美女三级做爰 | 精品国产一区二区三区久久久蜜月 | 狠狠干天天 | 性猛aa久久久 | 三人弄娇妻高潮3p视频 | 欧美一级美国一级 | 久久久日韩精品一区二区三区 | 国产一级二级在线播放 | 欧美成人性色 | 欧美日韩精品一区二区三区在线观看 | 片在线观看 | 久久精品久久精品久久精品 | 日韩毛片在线看 | 久久99精品久久久久久秒播蜜臀 | 99影视在线视频免费观看 | 欧美一级成人一区二区三区 | 久久精品亚洲国产奇米99 | 黄视频免费在线观看 | 久草欧美 | 草草在线观看 | 日本黄色成人 | 黄色成年在线观看 | 成人毛片100部免费观看 | 欧美一级高清免费 |