Introduction
Go is a statically typed language, meaning every variable has a fixed type known at compile time. Variables can be declared explicitly with the var keyword, or implicitly using the short declaration operator := inside functions, where Go infers the type from the assigned value. Go also gives every declared-but-unassigned variable a sensible zero value instead of leaving it undefined.
Cricket analogy: A player's role, batsman or bowler, is fixed on the team sheet before the toss just as a Go variable's type is fixed at compile time, while shorthand notes like 'VK' inferred to mean Kohli mirror the := operator inferring type from context.
Syntax
// var declaration with explicit type
var age int = 25
// var declaration with inferred type
var name = "Gopher"
// short declaration (function scope only)
count := 10
// multiple variables at once
var x, y int = 1, 2
a, b := "hello", trueExplanation
The var form is required at package level (outside any function), since := only works inside function bodies. Go's basic data types include bool, string, the signed integers int, int8, int16, int32, int64, the unsigned integers uint, uint8, uint16, uint32, uint64, the floating point types float32 and float64, and the complex types complex64 and complex128. byte is an alias for uint8 and rune is an alias for int32, used to represent a single Unicode code point. Any variable declared without an initial value automatically receives its type's zero value: 0 for numeric types, false for bool, "" for string, and nil for pointers, slices, maps, channels, functions, and interfaces.
Cricket analogy: Team sheet entries filed before the tournament (var, at package level) versus quick notes scribbled during a match (:= inside functions) parallel Go's rule, while a spare batsman with no assigned position defaults to zero runs, like a zero value.
Example
package main
import "fmt"
func main() {
var isActive bool
var temperature float64 = 36.6
var initial rune = 'G'
message := "Learning Go"
fmt.Println(isActive, temperature, initial, message)
fmt.Printf("Type of temperature: %T\n", temperature)
}Output
false 36.6 71 Learning Go
Type of temperature: float64Key Takeaways
- Use var for package-level declarations; use := only inside functions.
- Go infers types automatically when an initial value is provided.
- Unassigned variables get a zero value: 0, "", false, or nil.
- byte is an alias for uint8; rune is an alias for int32 (a Unicode code point).
- Every variable has one fixed, static type for its entire lifetime.
Practice what you learned
1. Which declaration form can only be used inside a function body?
2. What is the zero value of a Go string?
3. What is 'rune' an alias for in Go?
4. What is 'byte' an alias for in Go?
5. What does fmt.Printf's %T verb print?
Was this page helpful?
You May Also Like
Constants and iota in Go
Understand how const declarations work in Go and how iota generates auto-incrementing enumerated constants.
Type Conversion in Go
Learn why Go requires explicit type conversion between numeric and other types, and how to perform it safely.
Operators in Go
Explore arithmetic, comparison, logical, and bitwise operators in Go, including the unique AND NOT operator.
Input and Output in Go
Learn to print formatted output with fmt.Printf and read user input using fmt.Scanln and bufio.Reader.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics