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