In this tutorial you will learn about the Go Goto Statement and its application with practical example.
Go Goto Statement
In GoLang, goto statement is used to alter the normal execution of a program and transfer control to a labeled statement in the same program. The label is an identifier, it can be any plain text and can be set anywhere in the Go program above or below to goto statement. When a goto statement is encountered, compiler transfers the control to a label: and begin execution from there.
Syntax:-
1 2 3 4 |
goto label .. . label: |
Example:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package main import "fmt" func main() { var age int election: fmt.Print("Enter your age ") fmt.Scanln(&age) if (age <= 17) { fmt.Print("You are not eligible to vote.\n") goto election } else { fmt.Print("You are eligible to vote.") } } |
Output:-