This is a creation in Article, where the information may have evolved or changed.
If statement
i f x > 0 { // {是强制的,且必须和if在同一行 return y} else { return x}
If and switch accept initialization statements, which are typically used to set a (local) variable.
if err := Chmod(0664); err != nil { //nil 与 C 的 NULL 类似fmt.Printf(err) //err 的作用域被限定在 if 内return err}
Goto statement
Use Goto to jump to a label that must be defined within the current function
func myfunc() { i := 0 Here: // 这行的第一个词,以分号结束作为标签,标签名区分大小写 println(i) i++ goto Here // 跳转}
For statement
Go's for Loop has three forms, only one of which uses semicolons.
for init; condition; post { } // 和C的for一样for condition {} // 和while一样for {} // 死循环1、sum := 0for i:=0; i<10; i++ {sum+=i ←sum = sum + i 的简化写法 } 2、for i,j:=0, len(a)-1; i<j; i,j=i+1,j-1 {a[i], a[j] = a[j], a[i] //平行赋值 }
Break and Continue
Use Break to exit the loop prematurely, and break terminates the current loop.
for i:=0; i<10; i++ { if i>5{ break //终止这个循环,只打印 0 到 5 } println(i)}
When looping through nested loops, you can specify a label after break. Use a label to determine which loop is terminated:
J: for j:=0; j<5; j++ { for i:=0; i<10; i++ { if i>5{ break J // 现在种植的是j循环,而不是i的那个 } println(i) } }
Use continue to let the loop go to the next iteration and skip over all the remaining code. The following loop prints 0 to 5.
for i:=0; i<10; i++ { if i>5{ continue // 跳过循环中所有的代码println(i) } println(i)}
Range
Range can be used for loops, and support for slice, array, string, map, and Channel,range are iterators that, when called, return a key-value pair from the contents of the loop. Depending on the content, range returns different things.
// 遍历arraylist := []string {"a", "b", "c", "d", "e", "f"} for k, v := range list { // k,v键值对:k为key,v为value}// 遍历字符串for pos, char := range "aΦx" {fmt.Printf("character '%c' starts at byte position %d\n",char, pos)}
Switch statement
The switch of Go is very flexible. The expression does not have to be a constant or an integer, and the process is performed from top to bottom until a match is found, and if switch does not have an expression, it matches true. This creates a possibility--using switch to write if-else-if-else judgment sequences.
func unhex(c byte) byte { switch { case '0'<=c&&c<='9': return c - '0' case 'a'<=c&&c<='f': return c - 'a' + 10 case 'A'<=c&&c<='F': return c - 'A' + 10 } return 0 }
It does not match the failure automatically after the attempt is made, but you can use Fallthrough to make it do so. No Fallthrough:
switch i { case 0: // 空的case体case 1: f() // 当i==0时,f不会被调用!}
And this:
switch i { case 0: fallthrough case 1: f() // 当i==0时,f会被调用! }
Use default to specify the behavior when all other branches do not match.
switch i { case 0: case 1: f() default: g() //当i不等于0或1时调用}
A branch can use a comma-delimited list.
func shouldEscape(c byte) bool { switch c { case ' ', '?', '&', '=', '#', '+': // ,或者 “or”都可以 return true } return false}
Example of using switch to compare byte arrays:
func Compare(a, b []byte) int { for i:=0; i< len(a)&&i< len(b); i++ { switch { case a[i] > b[i]: return 1 case a[i] < b[i]: return -1 } } switch { 1 case len(a) < len(b): return -1 case len(a) > len(b): return 1 } return 0}