代码示例:html
sum := 0 for i, j := 0, 0; i <= 5 && j <= 5; i, j = i+1, j-1 { t.Log("i: ", i) t.Log("j: ", j) sum += i + j } t.Log("sum: ", sum)
Go中的控制语句较精简,仅有if、for、select和switch。但使用时均比较灵活函数
在Go中条件语句*if*中若是条件部分的计算结果为**true**时将执行语句块,不然则执行else语句块(若是存在else时),此逻辑和其余语言中的if同样,可是在Go中仍是有一些不一样之处。post
赋值+条件判断学习
if a, b := 21, 3; a > b { fmt.Println("a>b ? true") }
在if条件表达式前面声明的的变量只能在if-else语句块中使用。google
if a, b := 21, 31; a > b { fmt.Println("a>b ? true") }else { fmt.Println(a,b) //Ok } fmt.Println(a,b) //error: undefined a ,undefined b
还须要注意的是若是在if-else 中包含return 时,编译器没法解析出else中的retrun,致使方法缺乏return ,目前1.1版本已支持该方式。编码
func getName(id int) string { if id == 1 { return "YourName" }else { return "MyName" } //panic("") }
此代码编译不经过,错误信息:function ends without a return statement,这是在设计Go时故意这样的,也能够说是一个Bug(可参见:https://code.google.com/p/go/issues/detail?id=65),这是一种编码风格,即在if语句块中去作return处理,而else中不处理,而是继续执行if-else后面的代码,这样能减小一个代码缩进,不须要在了解代码时去记住else语句块的处理。固然若是想必须这样写,也能够进行特殊处理,在函数的末行添加语句**panic("")**spa
在if中能够包含初始化语句,这是很是实用的。例如在文件处理,取字典项时须要判断是否执行操做成功,只有在成功时才能继续处理,这样就能够经过if-else处理。设计
if err := file.Chmod(0664); err != nil { log.Print(err) return err } if v,err :=myMap[key];err != nil { log.Print(err) return err }else { //do something with v }
在Go中其余循环遍历的控制语句,惟有for。而for一样也是比较灵活的,for有三种形式。code
因为Go没有逗号表达式,而++和--是语句而不是表达式,若是想在for中执行多个变量,须要使用平行赋值htm
for i, j := 1, 10; i < j; i,j=i+1,j+1 { //死循环 fmt.Println(i) }
而不能写成
for i, j := 1, 10; i < j; i++,j++ { fmt.Println(i) }
for的condition在每执行一次循环体时便会执行一次,所以在实际开发过程当中须要注意不要让condition中计算简单而不是复杂。
for i,j :=0,len(str); i<j ; i++ { fmt.Println(str[i]) }
而不要写成(这仅是一个演示而已)
for i=0; i< len(str); i++ { fmt.Println(str[i]) }
另外for是遍历string,array,slice,map,chanel的方式,而使用保留字rang则能灵活的处理。rang是迭代器,能根据不一样的内容,返回不一样的东西。
须要注意的是for+rang遍历string时获得的是字节索引位置和UTF-8格式rune类型数据(int32)。
for pos, value := range "Go在中国" { fmt.Printf("character '%c' type is %T value is %v, and start at byte position %d \n", value,value,value, pos) str :=string(value) //convert rune to string fmt.Printf("string(%v)=>%s \n",value,str) } ---------OutPut------------ 一个汉字占三个字节 character 'G' type is int32 value is 71, and start at byte position 0 string(71)=>G character 'o' type is int32 value is 111, and start at byte position 1 string(111)=>o character '在' type is int32 value is 22312, and start at byte position 2 string(22312)=>在 character '中' type is int32 value is 20013, and start at byte position 5 string(20013)=>中 character '国' type is int32 value is 22269, and start at byte position 8 string(22269)=>国
另外在循环中使用break和continue,break用于退出循环,continue用于终止本次循环体的执行继续执行下一个循环。
sum := 0 for { if sum > 10 { break } sum += 2 } fmt.Println(sum) // sum=12
break也可退出指定的循环体
sum := 0 myforLable: for { for i := 0; i < 10; i++ { if sum > 200 { break myforLable //将退出循环体for{} } sum += i } } fmt.Println(sum)
switch是最灵活的一种控制语句,表达式能够不是常量或者字符串,也能够沒有表达式,若是没有表达式则如同if-else-else。
通常用法:和其余语言的Switch基本同样,不一样的不须要在每一个Case中添加Break,而是隐藏了Break,固然你能够显示加入break
switch ch { case '0': cl = "Int" case '1': cl = "Int" case 'A': cl = "ABC" break //能够添加 case 'a': cl = "ABC" default: cl = "Other Char" }
此段代码能够可写成(fallthrough表示继续执行下面的Case而不是退出Switch)
switch ch { case '0': fallthrough //必须是最后一个语句 case '1': cl = "Int" case 'A': case 'a': fallthrough cl = "ABC" //error default: cl = "Other Char" }
若是多个匹配结果所对应的代码段同样,则能够在一个case中并列出全部的匹配项
switch ch { case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9': cl = "Int" case 'A', 'B', 'C', 'D', 'a', 'b', 'c', 'd', 'e': cl = "ABC" default: cl = "Other Char" }
一样switch能够没有表达式,在 Case 中使用布尔表达式,这样形如 if-else
switch { case '0' <= ch && ch <= '9': cl = "Int" case ('a' <= ch && ch <= 'z') || ('A' <= ch && ch <= 'Z'): cl = "ABC" default: cl = "Other Char" }
下面是Switch写的一个示例(无实际意义):
func Compare(a, b interface{}) (int, error) { aT := reflect.TypeOf(a) bT := reflect.TypeOf(b) if aT != bT { return -2, errors.New("进行比较的数据类型不一致") } switch av := a.(type) { default: return -2, errors.New("该类型数据比较没有实现") case string: switch bv := b.(type) { case string: return ByteCompare(av, bv), nil } case int: switch bv := b.(type) { case int: return NumCompare(av, bv), nil } } return -2, nil }
还有另外一个控制语句select,在讨论chan 时再来学习
参考资料:
http://www.cnblogs.com/howDo/archive/2013/06/01/GoLang-Control.html