Core Syntax · 8 min read · Intermediate
Control Flow, Loops & Multiple Returns
Use Go's single loop keyword `for`, `if` with init statements, `switch` without fallthrough, and multiple return values.
What you'll learn
- Write counting, while-style, and range loops with for
- Return multiple values from functions cleanly
- Use switch without manual break statements
Overview
Use Go’s single loop keyword for, if with init statements, switch without fallthrough, and multiple return values.
Go prioritizes simplicity, explicit control flow, fast compilation, and safe concurrency. Every feature in this lesson is part of Go’s minimal standard toolkit used across cloud infrastructure, CLI tools, and high-throughput APIs.
Idiomatic Go Example
package main
import "fmt"
func divide(a, b int) (int, bool) {
if b == 0 {
return 0, false
}
return a / b, true
}
func main() {
if q, ok := divide(20, 4); ok {
fmt.Println("Quotient:", q)
}
}
Key Takeaways
- Write counting, while-style, and range loops with for
- Return multiple values from functions cleanly
- Use switch without manual break statements
Quick Check
Check Your Understanding
Score: 0 / 1
1. How does a `switch` case behave in Go by default?
Study Workspace
Finished this lesson?
Your progress is saved locally in your browser, no account needed.