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 阅读(150) 评论(0)  编辑 收藏 引用 所属分类: docker

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理