Variables & Constants in Go
Understand how variables and constants work in Go: declaration with var and :=, constants, scope, reassignment, and best practices.
Introduction to Variables
Variables are named containers used to store data so it can be reused and manipulated throughout a program. In Go, every variable has a type that is fixed at compile time.
1. What is a Variable?
Storing values in memory with a name and a type.
Declaring Variables
Go offers two main ways to declare variables: the `var` keyword for explicit declaration (with optional type and initial value) and the short declaration `:=` for local variables with type inference.
1. var Keyword
Explicit declaration. You can specify the type and optionally the initial value.
2. Short Variable Declaration (:=)
Inside a function, `:=` declares and assigns in one step. The type is inferred from the value.
3. Constants (const)
Constants cannot be reassigned. They must be known at compile time.
Reassignment vs Modifying Contents
Reassignment means changing which value a variable holds. Modifying contents means changing the internal state of a value (e.g. fields of a struct). Constants cannot be reassigned; variables can. For structs, you can still modify fields of a variable that holds a struct.
1. Reassigning Variables vs Modifying Struct Fields
A variable holding a struct can be reassigned to a new struct, or you can change its fields. Constants cannot be reassigned.
Declaration Before Use
In Go, variables must be declared before they are used; there is no hoisting. Using a variable before its declaration is a compile-time error.
1. Variables Must Be Declared Before Use
Accessing a variable before the line it is declared on causes a compile error.
2. Correct Declaration Order
Declare (and optionally initialize) first, then use.
Variable Scope
Scope determines the visibility and lifetime of a variable. In Go, variables are block-scoped: they exist from the line they are declared until the end of the innermost block (e.g. function body, if, for). Package-level variables are visible in all files of the same package.
1. Block Scope
Variables declared inside a block (e.g. if, for, or a pair of braces) are only visible inside that block.
2. Package-Level Scope
Variables (and constants) declared outside functions are visible in the whole package.
Redeclaration & Reassignment
In the same block, you cannot redeclare a variable with `var`. Short declaration `:=` can redeclare only in special cases (e.g. when at least one variable on the left is new). Variables can be reassigned; constants cannot.
1. Redeclaration Rules
You cannot declare the same variable twice in the same block with `var`. With `:=`, redeclaration is allowed only when it introduces at least one new variable (e.g. in a multi-value assignment).
2. Reassignment Rules
Variables declared with `var` or `:=` can be reassigned. Constants cannot.
Best Practices
Following a few guidelines makes Go code clearer and easier to maintain.
1. Recommended Guidelines
Common conventions in Go codebases.