Variables
Explore the fundamentals of Go variables, data types, and constants, including the impact of unused variables on compilation. Understand variable shadowing and learn how to write cleaner Go programs by following language-specific rules.
We'll cover the following...
We'll cover the following...
Unused variables
Go programs with unused variables do not compile:
“The presence of an unused variable may indicate a bug […] Go refuses to compile programs with unused variables or imports, trading short-term convenience for long-term build speed and program clarity.”
https://golang.org/doc/faq
Exceptions to that rule are global variables and function arguments:
Short variable declaration
Short variable declarations only work inside functions:
They also don’t work when setting field values: