Variable Scopes and Shadowing

Understand variable scopes and shadowing in Go, along with an overview of zero values.

A scope is part of the program in which a variable can be seen. In Go, we have the following variable scopes:

  • Package scoped: Can be seen by the entire package and is declared outside a function

  • Function scoped: Can be seen within {} which defines the function

  • Statement scoped: Can be seen within {} of a statement in a function (for loop, if/else)

Get hands-on with 1400+ tech skills courses.