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.")
}
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)
}
While-style Loop
i := 0
for i < 5 {
fmt.Println(i)
i++
}
Infinite Loop
for {
fmt.Println("Loop forever")
break // Donât forget to break!
}
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")
}
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")
}
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)
- 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")
}
}
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
}
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)