• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            zhiye_wang

            向星空仰望的越深,越發現自己的渺小

              C++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
              31 隨筆 :: 1 文章 :: 2 評論 :: 0 Trackbacks

            Go 語言教程筆記(

            一 Go語言決策

            if 語句

            if語句包含一個布爾表達式后跟一個或多個語句

            語法

            if語句在Go編程語言的語法是:

            if(boolean_expression)

            {

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

            如果布爾表達式的值為 true,那么if語句里面代碼塊將被執行。如果if語句的結束(右大括號后)

            布爾表達式的值為false,那么語句之后第一行代碼會被執行。

            例子:

            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代碼塊將被執行

            例子:

            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

            switch 語句

            switch語句可以讓一個變量對反對值的列表平等進行測試。每個值被稱為一個的情況(case),

            變量被接通檢查每個開關盒(switch case)。

            在Go編程,switch有兩種類型。

             表達式Switch - 在表達式switch,case包含相比較,switch表達式的值。

             類型Switch - 在這類型switch,此時含有進行比較特殊注明開關表達式的類型。

             表達式Switch

            在Go編程語言中表達switch語句的語法如下:

            switch(boolean-expression or integral type){     case boolean-expression or integral type  :        statement(s);           case boolean-expression or integral type  :        statement(s);      /* you can have any number of case statements */     default : /* Optional */        statement(s); }

            以下規則適用于switch語句:

            在switch語句中使用的表達式必須具有整體或布爾表達式,或者是一個類型,其中所述類具有

            一個單一的轉換函數,以一個整體或布爾值。如果表達不通過,默認值是true。可以有任意數

            量的case語句在switch內。每個case后跟值進行比較,以及一個冒號。constant-expression 

            的情況,必須是相同的數據類型,在switch的變量,它必須是一個常量或文字。

            當變量被接通等于case的值,以下case中將執行語句。在case語句中break不是必需。

            switch語句可以有一個可選默認情況下,它必須出現在開關結束。缺省情況下,可用于執行任

            務時沒有的case為true。則case在默認情況下也不是必須的。

            例子

            package main  import "fmt"  func main() {    /* local variable definition */    var grade string = "B"    var marks int = 90     switch marks {       case 90: grade = "A"       case 80: grade = "B"       case 50,60,70 : grade = "C"       default: grade = "D"      }     switch {       case grade == "A" :          fmt.Printf("Excellent!\n" )            case grade == "B", grade == "C" :          fmt.Printf("Well done\n" )             case grade == "D" :          fmt.Printf("You passed\n" )             case grade == "F":          fmt.Printf("Better try again\n" )       default:          fmt.Printf("Invalid grade\n" );    }    fmt.Printf("Your grade is  %s\n", grade );       }

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

            Well done Excellent! Your grade is  A

            類型Switch

            在Go編程語言的一個類型switch語句的語法如下:

            switch x.(type){     case type:        statement(s);           case type:        statement(s);      /* you can have any number of case statements */     default: /* Optional */        statement(s); }

            以下規則適用于switch語句:

            在switch語句中使用必須有接口的變量表達式{}輸入。

            在switch內可以有任意數量case語句。每一種case后跟的值進行比較,以及一個冒號。

            case 類型必須是相同的數據類型,在switch的變量,它必須是一個有效的數據類型。

            當變量被接通等于某一case中的值,以下case語句將執行。在case語句塊的break不是必需的。

            switch語句可以有一個可選默認case,它必須出現在switch的結束。缺省情況下,可用于執行

            任務時沒有匹配case時。default不是必需的。

            例子

            package main  import "fmt"  func main() {    var x interface{}          switch i := x.(type) {       case nil:	            fmt.Printf("type of x :%T",i)                       case int:	            fmt.Printf("x is int")                              case float64:          fmt.Printf("x is float64")                  case func(int) float64:          fmt.Printf("x is func(int)")                             case bool, string:          fmt.Printf("x is bool or string")              default:          fmt.Printf("don't know the type")         }    }

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

            type of x :<nil>

             select語句

            select語句的語法如下:

            select {     case communication clause  :        statement(s);           case communication clause  :        statement(s);      /* you can have any number of case statements */     default : /* Optional */        statement(s); }

            以下規則適用于select語句:

            可以有任意數量的范圍內選擇一個case語句。每一種情況下后跟的值進行比較,以及一個冒號。

            對于case的類型必須是一個通信通道操作。

            當通道運行下面發生的語句這種情況將執行。在case語句中break不是必需的。

            select語句可以有一個可選默認case,它必須出現在select的結束前。缺省情況下,可用于執行

            任務時沒有的情況下是真實的。在默認情況下break不是必需的。

            例如:

            package main  import "fmt"  func main() {    var c1, c2, c3 chan int    var i1, i2 int    select {       case i1 = <-c1:          fmt.Printf("received ", i1, " from c1\n")       case c2 <- i2:          fmt.Printf("sent ", i2, " to c2\n")       case i3, ok := (<-c3):  // same as: i3, ok := <-c3          if ok {             fmt.Printf("received ", i3, " from c3\n")          } else {             fmt.Printf("c3 is closed\n")          }       default:          fmt.Printf("no communication\n")    }     }   

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

            no communication

            posted on 2016-03-28 16:54 zhiye_wang 閱讀(171) 評論(0)  編輯 收藏 引用 所屬分類: docker
            色妞色综合久久夜夜| 国产精品久久久久久影院| 亚洲国产精品嫩草影院久久| 久久激情亚洲精品无码?V| 久久精品国产亚洲av麻豆图片| 一本色综合网久久| 91久久精品国产成人久久| 久久人妻AV中文字幕| 欧美日韩中文字幕久久伊人| 色狠狠久久综合网| 久久婷婷国产麻豆91天堂| 久久久黄色大片| 九九热久久免费视频| 久久强奷乱码老熟女网站 | 色婷婷综合久久久久中文一区二区 | 伊人久久五月天| 久久精品视频网| 久久久久久久人妻无码中文字幕爆| 国产精品99久久久久久猫咪 | 精品久久久一二三区| 久久青青草原综合伊人| 少妇人妻88久久中文字幕| 久久久久久A亚洲欧洲AV冫| 99久久精品国产高清一区二区| 久久亚洲AV无码精品色午夜麻豆| 久久九九久精品国产| 国产成人99久久亚洲综合精品| 国内精品久久久久久99蜜桃| 亚洲国产精品无码久久98| 国产一区二区久久久| 亚洲人AV永久一区二区三区久久| 国产精品成人久久久久三级午夜电影| 久久精品国产亚洲AV无码麻豆| 午夜不卡久久精品无码免费| 亚洲女久久久噜噜噜熟女| 久久精品国产亚洲AV蜜臀色欲 | 久久精品a亚洲国产v高清不卡| 人妻丰满AV无码久久不卡| 久久久噜噜噜久久中文福利| 久久青青草原精品国产| 日韩人妻无码精品久久免费一 |