Skip to content

Latest commit

 

History

History
70 lines (59 loc) · 1.13 KB

control_flow.md

File metadata and controls

70 lines (59 loc) · 1.13 KB

Overview

  • Control flow structures & idioms

Conditional

  1. Simple example
if x < 3 {
    return z
}
  1. Compound example, checks for error
if err := file.Chmod(0755); err != nil {
    return err
}

Loops (iteration)

  1. Like a while
for shouldContinue {
  // ...
}
  1. Run until explicit exit
for {
    // ...
    // break or return
}
  1. Fixed iterations
for i := 0; i < 10; i++ {
  // ...
}
  1. Map iteration: See maps doc
  2. Slice iteration: See slices doc

Switch

  1. No automatic fall thru
  2. General switch, alternative to if-elseif chains
switch {
case isBlue && isAlive:
    return "smurf"
case isYellow && isAlive:
    return "minion"
}
  • TODO: switch on enum
  • TODO: switch on character
  • TODO: switch on number
  • TODO: switch on string
  • TODO: type switch
  • TODO: switch: multiple cases (comma separated)

Idioms

  1. Rarely use else, use multiple returns instead

Other Resources

  1. https://gobyexample.com/if-else
  2. https://gobyexample.com/for