Lesson 4. Variable scope

After reading lesson 4, you’ll be able to

  • Know the benefits of variable scope
  • Use a shorter way to declare variables
  • See how variable scoping interacts with for, if, and switch
  • Know when to use a wide or narrow scope

In the course of running a program, many variables are used briefly and then discarded. This is facilitated by the scoping rules of the language.

Consider this

How many things can you keep in your head at once?

It has been suggested that our short-term memory is limited to about seven items, with a seven-digit phone number being an excellent example.

Computers can store many values in their short-term or Random Access Memory (RAM), but remember that code is read not only by computers, but ...

Get Get Programming with Go now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.