- Control flow structures & idioms
- Simple example
- Compound example, checks for error
if err := file.Chmod(0755); err != nil {
return err
}
- Like a
while
for shouldContinue {
// ...
}
- Run until explicit exit
for {
// ...
// break or return
}
- Fixed iterations
for i := 0; i < 10; i++ {
// ...
}
- Map iteration: See maps doc
- Slice iteration: See slices doc
- No automatic fall thru
- 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)
- Rarely use
else
, use multiple return
s instead
- https://gobyexample.com/if-else
- https://gobyexample.com/for