Go Loops: Control Flow

Glitch
3 min readMar 18, 2023

--

Control flow is a critical aspect of programming that allows developers to execute code based on specific conditions. Loops, in particular, are a type of control flow that allows developers to repeatedly execute code while a certain condition remains true. In this blog, we'll explore how Go handles loops as a control flow mechanism.

Go offers three types of loops: for, range, and goto. Let's take a closer look at each of these loops and how they can be used to control the flow of your code.

The for Loop

The for loop in Go is a versatile loop that can be used in a variety of situations. It has the following basic syntax:

for initialization; condition; post { // code to be executed }

The for loop consists of three parts: initialization, condition, and post. The initialization is executed only once, at the beginning of the loop. The condition is checked at the beginning of each iteration, and the loop continues as long as the condition is true. The post statement is executed at the end of each iteration.

Here's an example of a simple for loop in Go that prints the numbers from 1 to 10:

for i := 1; i <= 10; i++ { fmt.Println(i) }

In this example, the loop initializes i to 1, checks if i is less than or equal to 10, and then increments i by 1 at the end of each iteration.

The range Loop

The range loop is used to iterate over arrays, slices, maps, strings, and channels in Go. Its syntax is as follows:

for index, value := range collection { // code to be executed }

In this syntax, index is the index of the current element in the collection, and value is the value of the current element. collection can be an array, slice, map, string, or channel.

Here's an example of a range loop in Go that iterates over an array of strings:

fruits := [3]string{"apple", "banana", "orange"} for index, value := range fruits { fmt.Println(index, value) }

This loop will print the index and value of each element in the fruits array.

The goto Statement

The goto statement in Go allows developers to jump to a different part of the code, similar to a jump statement in assembly language. Its syntax is as follows:

less

goto label ... label: // code to be executed

In this syntax, label is a user-defined identifier that represents a point in the code. When the goto statement is executed, the program will jump to the label and execute the code that follows.

Here's an example of a goto statement in Go that jumps to a label called end:

for i := 1; i <= 10; i++ { if i == 5 { goto end } fmt.Println(i) } end: fmt.Println("End of loop")

In this example, the loop will print the numbers from 1 to 4, and then jump to the end label, which will print "End of loop".

Conclusion

Loops are an essential part of control flow in programming, and Go provides three different types of loops: for, range, and goto. Each of these loops has its own unique syntax and use cases, and understanding how to use them effectively can help you write more efficient and powerful code in Go.

--

--

No responses yet