Go Language goto Statement
The goto statement in Go language allows unconditional transfer to a specified line in the process.
The goto statement is usually used in conjunction with conditional statements. It can be used to implement conditional transfer, form loops, and exit loop bodies, among other functionalities.
However, in structured programming, the use of goto statements is generally not encouraged to avoid causing confusion in the program flow, making it difficult to understand and debug the program.
Syntax
The syntax for the goto statement is as follows:
goto label;
..
.
label: statement;
The flowchart for the goto statement is as follows:
Example
Skip the current iteration and return to the beginning of the loop at label LOOP when variable a
equals 15:
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("Value of a: %d\n", a)
a++
}
}
The output of the above example is:
Value of a: 10
Value of a: 11
Value of a: 12
Value of a: 13
Value of a: 14
Value of a: 16
Value of a: 17
Value of a: 18
Value of a: 19