var declaration
The Go statement that declares a variable with the var keyword, optionally giving its type and a starting value.
In more detail
Use var at package level, where := is not allowed, and when you want an explicit type or the zero value, as in var retries int64 = 3 or var enabled bool. An unused local variable is a compile error in Go, not a warning.
Examples
var retries int64 = 3var hosts []string // nil sliceRelated terms
- short variable declaration — The := form that declares a new variable and sets its value in one step, letting Go infer the type.
- zero value — The value Go gives a variable automatically when you declare it without one: 0 for numbers, "" for strings, false for booleans and nil for pointers, slices and maps.
- static typing — A language rule that every variable has one fixed type, known when the program is compiled, so type mistakes are caught before it runs.
Taught in
- Variables, Typing, and Zero Values — Concurrent Systems Programming with Go
- Pointers and Memory — Concurrent Systems Programming with Go
- Arrays, Slices, and Maps — Concurrent Systems Programming with Go
- Wrapping Errors and errors.Is — Concurrent Systems Programming with Go
- The Standard testing Package — Concurrent Systems Programming with Go
- Goroutines and Concurrent Execution — Concurrent Systems Programming with Go
- Files and the os Package — Concurrent Systems Programming with Go
- Streams and the io Package — Concurrent Systems Programming with Go