Appendix B. six things we didn’t cover: Leftovers


We’ve covered a lot of ground, and you’re almost finished with this book. We’ll miss you, but before we let you go, we wouldn’t feel right about sending you out into the world without a little more preparation. We’ve saved six important topics for this appendix.

#1 Initialization statements for “if”

Here we have a saveString function that returns a single error value (or nil if there was no error). In our main function, we might store that return value in an err variable before handling it:


Now suppose we added another call to saveString in main that also uses an err variable. We have to remember to make the first use of err a short variable declaration, and change later uses to assignments. Otherwise, we’ll get a compile error for attempting to redeclare a variable.


But really, we’re only using the err variable within the if statement and its block. What if there was a way to limit the scope of the variable, so that we could treat each occurrence as a separate variable?

Remember when we first covered for loops, back in Chapter 2? We said they could include an initialization statement, where you initialize variables. Those ...

Get Head First Go now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.