Go language goto statement

Go language loop statements

The goto statement in Go language can unconditionally transfer to a specified line in the procedure.

The goto statement is usually used together with conditional statements. It can be used to implement conditional transfer, form loops, jump out of loops, and other functions.

However, in structured programming design, the use of goto statements is generally not advocated, to avoid causing confusion in the program flow, making it difficult to understand and debug the program.

Syntax

The goto syntax format is as follows:

goto label;
..
.
label: statement;

The flow chart of the goto statement is as follows:

Example

When variable a equals 15, skip this iteration of the loop and return to the loop's start statement LOOP:

Example

package main

import "fmt"

func main() {
   /* Define local variables */
   var a int = 10

   /* Loop */
   LOOP: for a < 20 {
      if a == 15 {
         /* Skip iteration */
         a = a + 1
         goto LOOP
      }
      fmt.Printf(a's value is : %d\n", a)
      a++    
   }  
}

The execution result of the above example is:

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

Go language loop statements

other extensions