Table of contents

Go: loops for, break, continue, defer, if and else

Go: loops for, break, continue, defer, if and else

This entry will deal with loops in the go programming language.

Go handles loops a little differently than what you are used to. If you’re already fluent in any other programming language, you probably remember while, do while and for loops. And if you come from Python or Javascript, you’ll remember how useful for x in or for x of loops are.

Well, as there are more than enough loops for. Yes, there is no while or do while. But then how do I use the rest of the loops? Read on and I’ll explain.

If you don’t know anything about Go and want to start with the basics visit my entry Golang: introduction, variables and data types.

If you currently use Python and want to see how it is different from Go, visit my python vs go post.

If and else

If_ and else allow you to execute blocks of code conditionally and keep the same syntax as in almost all languages.

if true {
    fmt.Println("Verdadero")
} else {
    fmt.Println("Falso")
}

else if

And of course go also has an else if.

edad := 18
if edad < 18 {
    fmt.Println("Menor de edad")
} else if edad > 18 {
    fmt.Println("Mayor de edad")
} else {
    fmt.Println("Tiene 18 exactamente")
}

Loops for in go

In go there are several types of for loops: counter, conditional, range and infinite.

Loops with counter

This is the classic loop that you already know from Javascript, C++, etc. In which you declare a variable, specify a condition, and make changes to the variable.

We declare “i” equal to 0, as long as “i” is less than 10, it executes the next block and subsequently increments “i” by one, each instruction separated by a semicolon.

for i:= 0; i < 10; i++ {
    // ...
}

Loop with conditional

In this type of loop a condition is evaluated, if the result is true, the block is executed, if not, that block of code is skipped. This type of for loop would be the equivalent of the while loop.

counter := 0
for counter < 10 {
    counter ++
}

Loop with range

Range allows us to traverse an iterable structure from beginning to end and allows us to access the respective index and element. It is ideal for traversing arrays, strings, slices, maps, channels and any structure that can be traversed.

HolaMundo := "Hola mundo"
    for index, letra := range HolaMundo {
    	fmt.Printf("%d %c \n", index, letra)
    }
/*
0 H 
1 o 
2 l 
3 a 
4   
5 m 
6 u 
7 n 
8 d 
9 o*/

Infinite loop

A for loop without condition will run forever.

The only way to get out of an infinite for loop is with a break.

counterForever := 0
for {
    counterForever++
}

break

As I just mentioned, break breaks a loop and continues code execution.

counterForever := 0
for {
    counterForever++
    fmt.Println(counterForever)
    if counterForever > 5 {
        fmt.Println("Aquí se rompe el bucle")
        break
}
}
// 1
// 2
// 3
// 4
// 5
// 6
// Aquí se rompe el bucle

Break in loops with name in go

In other programming languages, such as Python, break would break the immediate loop, i.e. the immediate loop it is in. Wouldn’t it be great to be able to stop the outer loop from the inner loop? In go it is possible in a simple way

while True:
    while True:
        break
    print("El bucle principal continua ejecutándose")
# El bucle principal continua ejecutándose
# El bucle principal continua ejecutándose
# El bucle principal continua ejecutándose

Go allows us to assign names to the loops, what for? To reference specific loops and be able to break them using break. Take a look at this example:

loop:
    for {
    	for {
    		fmt.Println("Rompiendo el bucle externo")
    		break loop
    	}
    	fmt.Println("Esto nunca se imprimirá en pantalla")
    }

We name our loop as loop and now we execute an infinite loop that will have an infinite loop inside it. This last loop will break the outer loop, named loop, so the second statement will never be printed on the screen.

continue

Continue stops the current iteration of the loop and continues its execution in the next iteration.

counter := 0
for counter < 10 {
    counter ++
    if counter == 3 {
        fmt.Println("Nos brincamos el 3")
        continue
    }
    fmt.Println(counter)
}
//1
//2
//Nos brincamos el 3
//4

defer

Defer delays the execution of a line of code until the end of the code. It is quite similar to what the defer attribute with HTML script tag does.

defer fmt.Println("Esto se va a ejecutar hasta el final")
    fmt.Println("Esto se va a ejecutar primero")
    fmt.Println("Esto se va a ejecutar después")
// Esto se va a ejecutar primero
// Esto se va a ejecutar después
// Y esto se va a ejecutar hasta el final

What’s the point? Well, it is ideal to close connections to databases, files or to make some type of cleaning to the objects of our code.

const conexionBaseDeDatos := abreBaseDeDatos()
defer cierraBaseDeDatos()
queryBaseDeDatos()
otraQuery()

For the next Go entry I’m going to talk about the basics of slices, arrays and maps.

Remember that you can visit the official go documentation if there is anything you want to learn more about.

Eduardo Zepeda
Web developer and GNU/Linux enthusiast always learning something new. I believe in choosing the right tool for the job and that simplicity is the ultimate sophistication. I'm under the impression that being perfect is the enemy of getting things done. I also believe in the goodnesses of cryptocurrencies outside of monetary speculation.
Read more