Controlling Program Flow In Go

Controlling Program Flow In Go

Gain working knowledge on conditionals, loops and cases.

Gain working knowledge on conditionals, loops and cases.

Go has a unique take on controlling the flow of your program. In fact, there are no combinations of do loops or while loops. That’s because Go exclusively uses for loops! However, this does not stop a developer from being able to create flexible conditionals to execute. If anything, it is a powerful simplification of the developer’s control flow toolbox.

The Powerful For Loop Experience

Let’s start with some examples of the variety of for loop controls in Go. The classic example of printing out some numbers looks like this:

package main

import "fmt"
func main() {
    var i int
    for i < 10 {
        fmt.Println(i)
        i++
    }
}

There’s a few things going on here. First we initialize i as an int, then we create a for loop that checks if i is less than 10. This program would print out 0 through 9, since 0 is the default value of an int. Also notice how the incrementation of i isn’t built in, we have to do that ourselves. In a way, this basic for loop is almost like an if-statement itself!

If we want to break out of the loop early, we can simply add an if-statement within the loop.

for i < 10 {
    fmt.Println(i)
    if i == 5 {
        break
    }
}

You can also use the continue statement if, you guessed it, you just want to continue your program.

for i < 10 {
    fmt.Println(i)
    i++
    if i == 5 {
        fmt.Println("continuing...")
        continue
    }
}

golang programming tutorial coding

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

13 Free/Low-Cost Sites to Supercharge Your Programming Self-Education

Although we still talk about programming as a standalone career, the dominance of technology in our lives makes it clear that coding is much more than a career path. In my opinion, computer science is more than a college major or a high-paid job; it’s a skill, essential for thriving in a modern-day economy. Whether you work in healthcare, marketing, business, or other fields, you will see more coding and have to deal with a growing number of technologies throughout your entire life.

How to Structure Your Programming Code

I remember my first fumble with basic on my ZX Spectrum computer back in the 1980s, ploughing through pages of basic commands and example code without any real idea of how I could write programs myself

What Is Code Golfing And Biggest Such Tournaments

Code golf is a type of recreational computer programming competition in which participants strive to achieve the shortest possible source code that implements a certain algorithm. Playing code golf is known as "golf scripting". Code golf challenges and tournaments may also be named with the ... Some code golf questions, such as those posed on general programming ...

Coding 101: Programming Language Building Blocks

This article will introduce the concepts and topics common to all programming languages, that beginners and experts must know!

Static Code Analysis: What It Is? How to Use It?

Static code analysis is a method of debugging by examining source code before a program is run. It's done by analyzing a set of code against a set (or multiple sets) of coding rules. Static code analysis and static analysis are often used interchangeably, along with source code analysis.