Latest web development tutorials

Go language goto statement

Go Language loop Go Language loop

Go language goto statement unconditionally transferred to the process specified line.

goto statement is generally used in conjunction with conditional statements. It can be used to implement a conditional branch, constitute cycle, out of the loop body functions.

However, in the structure of the program design generally do not advocate the use of the goto statement, program flow in order to avoid confusion, make comprehension difficult to produce and debug programs.

grammar

goto syntax is as follows:

goto label;
..
.
label: statement;

break statement is a flow chart is as follows:

Examples

package main

import "fmt"

func main() {
   /* 定义局部变量 */
   var a int = 10

   /* 循环 */
   LOOP: for a < 20 {
      if a == 15 {
         /* 跳过迭代 */
         a = a + 1
         goto LOOP
      }
      fmt.Printf("a的值为 : %d\n", a)
      a++     
   }  
}

The above examples Implementation of the results:

a的值为 : 10
a的值为 : 11
a的值为 : 12
a的值为 : 13
a的值为 : 14
a的值为 : 16
a的值为 : 17
a的值为 : 18
a的值为 : 19

Go Language loop Go Language loop