← Go Programming learning pathGo Programming · Module 3 of 10
Control flow
Use if, for and switch in Go.
📖 Key ideas
- Go has only one loop keyword: for.
- for i := 0; i < 5; i++ { } or for condition { } (like while).
- Range loops: for i, v := range slice.
- if can include a short statement: if err := f(); err != nil { }.
- switch does not fall through by default.
💡 Real-life example: Go's for is a Swiss Army knife: one tool that works as for, while and forever loops.
forThe only loop in Go
rangeIterates over collections
switchMulti-branch statement
deferRuns a call when the function returns