Summary
At this point, you know how Go differs from other programming languages. For instance, Go doesn't require you to write parentheses in conditions for if
, for
, or switch
statements. But you always need to write braces ({}
). You can chain if
statements, and an else
clause is optional. Crucially, you can declare variables within an if
condition, and their scope is only within the if
block. You can't access those variables outside the block, even in the same function.
Go supports switch
statements, and you don't have to write a condition. You can use just case
clauses. Unlike other languages, in Go you don't need to write a break
statement at the end of each case
clause to avoid running other case
clauses.
By default, when Go enters into a case
statement, it runs it and then exits the switch
clause. To jump to the next case
clause, use the fallthrough
keyword. You can call a function from a case
clause, and you can group multiple expressions in one case
clause.
In this module, you also learned that in Go you use only the for
keyword to write loops. But you can write infinite loops or while
conditions. Go supports the continue
keyword, so you can skip an iteration of a loop without exiting the loop.
Lastly, you learned about other Go control flows like the defer
, panic
, and recover
functions. Go doesn't support exceptions. It deals with runtime errors by using a combination of the three functions.