← Go Programming learning path
Go Programming · Module 3 of 10

Control flow

Use if, for and switch in Go.

📖 Key ideas

  1. Go has only one loop keyword: for.
  2. for i := 0; i < 5; i++ { } or for condition { } (like while).
  3. Range loops: for i, v := range slice.
  4. if can include a short statement: if err := f(); err != nil { }.
  5. 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