Go control flow is C-like: if, for, and switch. There is no while keyword—loops use for only. Parentheses around conditions are optional but braces are required.
if and for
if count > 0 {
fmt.Println("positive")
}
for i := 0; i < 3; i++ {
fmt.Println(i)
}
if may include a short statement before the condition: if err := f(); err != nil { ... }—common in Go error handling.
switch
switch does not fall through by default (no implicit break). Use fallthrough explicitly when needed. Switch works on types in type switches later.
Important interview questions and answers
- Q: while loop in Go?
A: Usefor condition { }—there is no separate while keyword. - Q: Does switch fall through?
A: No by default—each case breaks automatically unlessfallthroughis used.
Self-check
- Write a for loop that prints 0, 1, 2.
- What keyword breaks out of a switch case automatically?
Tip: Only for loops exist—for {} is the while loop; braces are mandatory even for one-line bodies.
Interview prep
- while in Go?
Use
for condition { }—there is no separate while keyword.- switch fallthrough?
Cases break automatically; use explicit
fallthroughwhen needed.