Go lets you declare variables in lots of places. You can declare them outside of functions, as the parameters to functions, and as local variables within functions.
- 02.01BlocksGo lets you declare variables in lots of places. You can declare them outside of functions, as the parameters to functions, and as local variables within functions.
- 02.02Branching with ifThe if statement in Go is much like the if statement in most programming languages. Because it is such a familiar construct, I’ve used it in previous sample code without…
- 02.03Code Review: Blocks, shadowing, and if statementsWatch for accidental shadowing when using := in inner scopes.
- 02.04Practice: Blocks, shadowing, and if statementsThe following exercises let you try out code with blocks and if statements.
- 02.05for, Four WaysAs in other languages in the C family, Go uses a for statement to loop. What makes Go different from other languages is that for is the only looping keyword in the language. Go…
- 02.06The Complete for StatementThe first for loop style is the complete for declaration you might be familiar with from C, Java, or JavaScript, as shown in Example 3-8.
- 02.07The Condition-Only for StatementWhen you leave off both the initialization and the increment in a for statement, do not include the semicolons. (If you do, go fmt will remove them.) That leaves a for…
- 02.08The Infinite for StatementThe third for statement format does away with the condition, too. Go has a version of a for loop that loops forever. If you learned to program in the 1980s, your first program…
- 02.09break and continueHow do you get out of an infinite for loop without using the keyboard or turning off your computer? That’s the job of the break statement. It exits the loop immediately, just…
- 02.10The versatile for-range StatementThe fourth for statement format is called a for-range loop and resembles the iterators found in other languages. It is for iterating over elements in some of Go’s built-in…
- 02.11Labeling Your for StatementsBy default, the break and continue keywords apply to the for loop that directly contains them. What if you have nested for loops and want to exit or skip over an iterator of an…
- 02.12Choosing the Right for StatementNow that I’ve covered all forms of the for statement, you might be wondering when to use which format. Most of the time, you should use the for-range format. A for-range loop…
- 02.13Code Review: for loopsPrefer for-range loops, especially when you’re iterating over every element in a compound type. They are also the easiest way to walk a string by runes or count up to a…
- 02.14Practice: for loopsThe following exercises let you write your own for loops.
- 02.15Choosing with switchLike many C-derived languages, Go has a switch statement. Most developers in those languages avoid switch statements because of their limitations on values that can be switched…
- 02.16goto—Yes, gotoGo has a fourth control statement, but chances are you’ll never use it. Ever since Edsger Dijkstra wrote “Go To Statement Considered Harmful” in 1968, the goto statement has…
- 02.17Code Review: switch and gotoA break keyword only breaks out of its immediate scope. If you have a switch statement inside a for loop, a break in one of the switch statement’s case clauses breaks out of…
- 02.18Practice: switch and gotoThe following exercises let you explore switch statements and goto.
- 02.19Recap: Blocks, Shadows, and Control StructuresThis topic covered a lot of important topics for writing idiomatic Go. You’ve learned how to use blocks, shadowing, and control structures correctly, and to write simple Go…