goto statements
There may come a time when you want to skip certain logic within a function and go to a certain location within the function using the goto
keyword. This may be accomplished using a label within the function and will result in a compilation error if attempting to use the goto
label outside of the function scope.
goto
statements are a way of adapting the Go control flow; however, they need to be used with caution as they can lead to difficulties in understanding function control flow and decrease code readability if used improperly. goto
statements are used within the Go standard library in some cases, such as the math
package, to make logic easier to read and reduce the need for unnecessary variables.
Exercise 2.12 – using goto statements
In this exercise, we’ll use goto
in a function to show you how you can take control of it. We’re going to create a loop that keeps looping forever. This means we will have to stop it and exit manually...