Control flow and functions

Decisions with if and switch, loops with for, and functions that return several values.

About 4 hours of study

Learning objectives

Lessons

  1. 1

    Conditions and switch

    Making decisions with if, else if and else, comparison and logical operators, the if statement with a short setup, and switch with several values per case or no value at all.

  2. 2

    Loops with for

    Go's only loop keyword, for, in its three forms: with a counter, with just a condition, and forever; plus break, continue, nested loops and range over a string.

  3. 3

    Functions and multiple return values

    Writing functions with parameters and results, returning several values, named results, variadic parameters, functions as values, closures, recursion and defer.

Module practice

Module quiz

1. Which keyword makes a loop in Go?
2. Does a Go switch case run on into the next case?
3. What does a function with the results (int, error) return?
4. How do you leave an endless for loop?
5. What does the parameter nums ...int mean?
6. What does defer do?

‹ Go basicsSlices, maps and strings ›