Updated April 5, 2023
Introduction to Go goto
From the name itself displaying its purposes, goto which means we are trying to give instruction to the execution of the program to go to a particular statement from execution, in more technical words goto allows developers to jump from one statement to another statement at any time of executions of the program, which means with the help of the goto statement we can execute any loop in a different way, for example, if we are checking an array of student details and we want the loop to avoid execution of certain fixed roll numbers then we can set got for those roll numbers in loop and loop will goto again starting.
Syntax:
See in the below syntax we have written statement before the specific level 2, which means we want the program to jump to Lebel 2 during the execution of the code. This statement gives us the flexibility to switch from one statement to another. See the below simple syntax.
goto label 2;
Lebel1
Lebel 2
….
Lebel n
Flowchart
Below is the flowchart for the statement, where we are looking at its jumping from one statement to another with the help of the statement. Let us discuss the diagram in more elaborated ways.
- In the below diagram first execution of the program happens from the start and statements will be executed.
- Each time the execution happens and statement 1, statement 2, and statement 3 will execute.
- Suppose in the meanwhile we wanted to jump from statement 1 to statement 3 without executing statement 2 then what we can de? We can use the goto statement.
- See in the below diagram we are jumping from statement 1 to statement 3 with the help of a goto statement.
- In the go-to, we have written goto statement 3.
- In the same way, if we want our program to jump from statement 3 to statement 1 we can use the goto statement 1.
- Using too many statements may create confusion for other developers who are debugging as we can see flow will not be in sequence.
Please see the below diagram and the flow of how we are switching from one statement to another.
<img>
How goto the statement works in the Go language?
Before understanding the flow of the working of the goto statement in the go language we need to understand the basic things behind its uses, suppose you are running a for loop over a certain registration number of students of the student and you do not want to perform any operation if you get some particular registration number instead you want to start again loop with next registration number in such type of the cases we can use the concept of the goto statement.
- Once we write the goto statement go compiler expect for the level where we want to jump from the current label.
- Each jump from one statement to another will not consider the missing label between the flow of execution of the program.
- Hence we can understand that the goto statement allows us to run any loop the non-normal way or execute the loop in the way it is not designed for.
- So by using goto unnecessary it will be very hard for debugging as we mentioned it will not go throw the normal flow of execution of the program.
- I will not recommend using unnecessary goto statements because tracking will be very difficult.
Examples to Implement
To run or execute the examples just create the file with name main.go, you can give any name to the file we have given name main.go. And paste the example codes on the file and run the command go run main.go and output will be displayed.
Example #1
In the below example we are trying to print all the numbers except 17 so so we have written a condition if the value of the variable is equal to 17 goto LOOP, which means the loop will start from the next number to the 17. Please see the below example along with the screen of the output.
Code:
package main
import "fmt"
func main() {
var i int = 10
LOOP: for i < 25 {
if i == 17 {
i = i + 1
goto LOOP
}
fmt.Printf("The value the variable i is: %d\n", i)
i++
}
}
Output:
Example #2
In the below example we are printing numbers except for those numbers which are divisible with 3. So in the below example, we have taken a condition of I %3==0, which means we are checking if the number is divisible with the 3, and if divisible then we are adding 2 to the variable I and jumping for the next statement of the loop. Please see the below example along with the screen of the output.
Code:
package main
import "fmt"
func main() {
var i int = 10
LOOP: for i < 30 {
if i%3 == 0 {
i = i + 2
goto LOOP
}
fmt.Printf("The value of i with non divisible with 3 is: %d\n", i)
i++
}
}
Output:
Conclusion
From this tutorial we learned some basic concepts of the go language goto statement, we learned its works with the help of a diagram and also we focus on some of the important examples. We discussed where to use and where not use the statement.
Recommended Articles
We hope that this EDUCBA information on “Go goto” was beneficial to you. You can view EDUCBA’s recommended articles for more information.