# if / else
Go's if doesn't need parentheses around the condition, but braces are always required.
package main
import "fmt"
func main() {
age := 20
if age >= 18 {
fmt.Println("Adult")
} else if age >= 13 {
fmt.Println("Teenager")
} else {
fmt.Println("Child")
}
}if with Init Statement
Go lets you run a short statement before the condition. The variable is scoped to the if/else block:
if err := doSomething(); err != nil {
fmt.Println("error:", err)
} else {
fmt.Println("success")
}# for — The Only Loop
Go has only one loop keyword: for. It replaces while, do-while, and traditional for from other languages.
Classic for loop
for i := 0; i < 5; i++ {
fmt.Println(i)
}While-style loop
n := 1
for n < 100 {
n *= 2
}
fmt.Println(n) // 128Infinite loop
for {
// runs forever until break or return
break
}for range
Iterate over slices, maps, strings, and channels:
names := []string{"Go", "Rust", "Python"}
for i, name := range names {
fmt.Printf("%d: %s\n", i, name)
}# switch
Go's switch is cleaner than C/Java: no automatic fall-through, and cases can be expressions.
day := "Tuesday"
switch day {
case "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday":
fmt.Println("Weekday")
case "Saturday", "Sunday":
fmt.Println("Weekend!")
default:
fmt.Println("Unknown")
}Tagless switch (like if/else chain)
score := 85
switch {
case score >= 90:
fmt.Println("A")
case score >= 80:
fmt.Println("B")
case score >= 70:
fmt.Println("C")
default:
fmt.Println("F")
}# break, continue & Labels
// Labeled break exits the outer loop
Outer:
for i := 0; i < 3; i++ {
for j := 0; j < 3; j++ {
if i == 1 && j == 1 {
break Outer
}
fmt.Printf("%d,%d ", i, j)
}
}
// Output: 0,0 0,1 0,2 1,0⚡ Key Takeaways
- No parentheses around conditions, but braces are required
foris the only loop — it handles classic, while-style, infinite, and range patternsswitchauto-breaks; usefallthroughexplicitly if needed- Init statements in
ifandswitchscope variables tightly - Labels +
break/continuecontrol nested loops