Skip to Content
Head First Go
book

Head First Go

by Jay McGavren
April 2019
Beginner
556 pages
14h 21m
English
O'Reilly Media, Inc.
Content preview from Head First Go

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

image

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:

image

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.

image

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 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Head First Git

Head First Git

Raju Gandhi
Learning Go

Learning Go

Jon Bodner
Head First Software Architecture

Head First Software Architecture

Raju Gandhi, Mark Richards, Neal Ford
Head First Design Patterns, 2nd Edition

Head First Design Patterns, 2nd Edition

Eric Freeman, Elisabeth Robson

Publisher Resources

ISBN: 9781491969540Errata Page