Naming values
Every program works with values. Some of those values change as the program runs, like a counter that increments or a status that flips from pending to complete. Others never change, like a maximum retry count, the value of pi or the name of an application.
Go gives you two tools for naming values: variables for things that change, and constants for things that do not.
Variables
A variable is a named storage location. Its value can be reassigned at any point during the program's execution.
The var keyword
The most explicit way to declare a variable is with the var keyword, followed by the name, the type, and optionally an initial value:
var age int // declared with zero value: 0var name string // zero value: ""var active bool // zero value: falsevar score int = 100 // declared with explicit valueWhen you declare a variable without assigning a value, Go initializes it to its zero value, the default value for that type. Every type has one: 0 for integers, 0.0 for floats, false for booleans, "" for strings. There is no such thing as an uninitialized variable in Go.
If you provide an initial value, Go can infer the type, so you do not need to name it explicitly:
var age = 30 // inferred as intvar greeting = "Hello" // inferred as stringShort variable declaration
Inside a function, you can omit both var and the type using the := operator. It declares and initializes the variable in one step:
age := 30greeting := "Hello"isActive := trueThe type is always inferred from the right-hand side. Short declarations are the most common form of variable declaration in Go because they are concise and keep functions readable.
:= only works inside functions. You cannot use it at package level:
package maincounter := 0 // compile error: not inside a functionfunc main() { counter := 0 // fine}Reassigning and reusing :=
:= requires that at least one variable on the left side is new. This allows you to reuse it when declaring a new variable alongside an existing one:
x := 10x, y := 20, 30 // valid: y is new, x is reassignedIf all variables on the left already exist, use plain = for reassignment:
x := 10x = 20 // reassignment, not a new declarationMultiple variables
You can declare multiple variables on a single line:
var x, y, z int = 1, 2, 3a, b := "hello", trueOr group them inside a var block, which is common for related declarations at the top of a function or at package level:
var ( host = "localhost" port = 8080 timeout = 30)Package-level vs function-level
Variables can be declared at two scopes:
Package-level variables are declared outside any function. They are accessible throughout the entire package and initialized before main runs.
Function-level variables are declared inside a function. They exist only for the duration of that function call and are invisible outside it.
package mainvar serverHost = "localhost" // package-levelfunc connect() { port := 5432 // function-level fmt.Println(serverHost, port)}Constants
A constant is a named value that cannot change after it is set. Go evaluates it entirely at compile time, and it never occupies a memory location at runtime.
The const keyword
Constants are declared with const:
const maxRetries = 3const appName = "MyApp"const pi = 3.14159265358979Like variables, constants support grouped declarations:
const ( maxRetries = 3 timeout = 30 appName = "MyApp")What can be a constant
Not every value qualifies as a constant. The compiler must be able to determine its value before the program runs. Valid constant values include:
- Boolean literals:
true,false - Integer, float, and complex literals
- String and rune literals
- Expressions that can be evaluated at compile time:
1 << 8,2 * 3.14,1 < 2 - Calls to certain built-in functions:
len,cap,real,imag,complex
const bufferSize = 1 << 8 // 256const isDebug = falseconst greeting = "Hello"const alwaysTrue = 1 < 2 // evaluated at compile time — trueFunction calls that produce runtime values, like reading from a file or calling time.Now(), cannot be used as constants.
Typed and untyped constants
Constants in Go come in two forms: typed and untyped.
A typed constant carries an explicit type and can only be directly assigned to a variable of that same type:
const maxRetries int = 3var x int = maxRetries // finevar y int64 = maxRetries // compile error: int is not int64An untyped constant has no fixed type. It carries a kind (integer, float, string, or boolean) and adapts to whatever type is expected at the point of use:
const maxRetries = 3 // untyped integer constantvar x int = maxRetries // finevar y int64 = maxRetries // fine — adapts to int64var z float64 = maxRetries // fine — adapts to float64iota
When you need a sequence of related integer constants, Go provides iota, a counter that starts at zero and increments by one with each entry in the block:
const ( Sunday = iota // 0 Monday // 1 Tuesday // 2 Wednesday // 3 Thursday // 4 Friday // 5 Saturday // 6)iota resets to zero at the start of every new const block. It is the idiomatic Go way to define enumerations.
Variables vs constants
Both variables and constants can be declared at package or function level, and both support grouped declarations with braces. They differ in four ways:
var | const | |
|---|---|---|
| Value can change | yes | no |
| Evaluated at | runtime | compile time |
Short form (:=) | yes (functions only) | no |
| Can remain unused | no (local only) | yes |
A local variable that is declared but never referenced is a compile error. Constants are always exempt, so they can exist purely for documentation or future use without the compiler objecting.