DEV Community

Cover image for GoLang 101: Making Decisions and Loops with Control Flow
Kazem
Kazem

Posted on

GoLang 101: Making Decisions and Loops with Control Flow

Welcome back to the third part of our Go journey! 🚀

So far, you’ve learned how to set up Go, write simple programs, work with variables, and define functions. Now it’s time to make our code dynamic—reacting to user input, making decisions, and repeating tasks.

Let’s explore Go’s control structures in a way that feels natural and easy to use.


The if Statement

At the core of every program is decision-making. In Go, if is your best friend.

score := 85

if score >= 90 {
    fmt.Println("Excellent!")
} else if score >= 70 {
    fmt.Println("Good job.")
} else {
    fmt.Println("Keep practicing.")
}
Enter fullscreen mode Exit fullscreen mode

No parentheses needed around the condition (unlike C or Java), and curly braces {} are required even for single-line blocks. Clean and strict—just the Go way.

Loops with for

Go has one loop keyword: for. That’s right—no while, no do-while. Just for, and it does everything.

Classic Loop

for i := 0; i < 5; i++ {
    fmt.Println(i)
}
Enter fullscreen mode Exit fullscreen mode

While-style Loop

i := 0
for i < 5 {
    fmt.Println(i)
    i++
}
Enter fullscreen mode Exit fullscreen mode

Infinite Loop

for {
    fmt.Println("Loop forever")
    break  // Don’t forget to break!
}
Enter fullscreen mode Exit fullscreen mode

Go’s loop syntax is super flexible. It’s easy to write readable code without weird surprises.

The switch Statement

Tired of long if-else chains? Use switch.

day := "Tuesday"

switch day {
case "Monday":
    fmt.Println("Start of the week")
case "Friday":
    fmt.Println("Almost weekend!")
default:
    fmt.Println("Just another day")
}
Enter fullscreen mode Exit fullscreen mode

Go’s switch automatically breaks after each case—no break needed.

You can even switch on conditions:

score := 82

switch {
case score >= 90:
    fmt.Println("A")
case score >= 80:
    fmt.Println("B")
default:
    fmt.Println("C or below")
}
Enter fullscreen mode Exit fullscreen mode

This is super helpful when writing clean conditional logic.

Getting User Input (Basic Way)

Want your Go program to interact with the user? Let’s read some input:

var name string
fmt.Print("Enter your name: ")
fmt.Scanln(&name)
fmt.Println("Hello,", name)
Enter fullscreen mode Exit fullscreen mode
  • fmt.Scanln() reads user input.
  • You must pass a pointer (&name) so Go can store the value.

Combining It All: A Tiny Program

Let’s write a simple interactive grading app:

package main

import (
    "fmt"
)

func main() {
    var score int
    fmt.Print("Enter your score: ")
    fmt.Scanln(&score)

    switch {
    case score >= 90:
        fmt.Println("Grade: A")
    case score >= 80:
        fmt.Println("Grade: B")
    case score >= 70:
        fmt.Println("Grade: C")
    default:
        fmt.Println("Grade: F")
    }
}
Enter fullscreen mode Exit fullscreen mode

Try running this in your terminal and inputting different numbers. You’re writing real logic now!


A Note on Error Handling

Go doesn’t use exceptions like many other languages. Instead, functions often return a value and an error.

You’ll see this pattern everywhere:

value, err := someFunction()
if err != nil {
    // handle the error
}
Enter fullscreen mode Exit fullscreen mode

We’ll explore error handling more in an upcoming lesson, but just keep this pattern in mind.


In this article, you learned how to:

  • Use if, else if, and else to make decisions
  • Write all kinds of loops using for
  • Simplify logic with switch (and conditional switches!)
  • Accept user input from the terminal using fmt.Scanln

In the next articles, we’ll dive into arrays, slices, and maps—Go’s most useful tools for working with collections of data.

You’re doing great. Keep practicing, keep exploring, and Go will soon feel like second nature.

Happy coding! 🧑‍💻

Top comments (0)