Variables and Types in Go
Declaring variables, Go's built-in types, and how type inference keeps static typing from feeling verbose.
2 min read
Go is statically typed — every variable has a fixed type, known at compile time. That sounds like it should mean writing types everywhere, but Go's type inference keeps the ceremony surprisingly light.
Declaring variables
The explicit way uses var:
var age int = 32
var name string = "Ada"
var isActive bool = trueBut inside a function, you'll almost always use the short declaration operator := instead, which infers the type from the value on the right:
age := 32
name := "Ada"
isActive := trueBoth produce identical results — age is an int either way. The difference is purely how much you type. := only works for declaring new variables inside a function body; package-level variables (declared outside any function) must use var.
Go's basic types
var i int // platform-sized integer (32 or 64-bit)
var i8 int8 // explicitly sized: int8, int16, int32, int64
var u uint // unsigned integer
var f float64 // floating-point (float32 also exists)
var s string // UTF-8 text
var b bool // true or false
var by byte // alias for uint8, used for raw bytes
var r rune // alias for int32, represents a single Unicode code pointMost everyday code just uses int, float64, string, and bool. The sized variants (int8, int32, uint16, and so on) matter when you're working with binary data, network protocols, or need to control exactly how much memory a value takes.
Zero values, not "undefined"
Every type in Go has a default zero value, and a declared-but-unassigned variable is never left in an undefined or garbage state:
var count int // 0
var price float64 // 0.0
var label string // "" (empty string)
var found bool // falseThis eliminates an entire class of bugs common in languages where an uninitialized variable can hold unpredictable memory contents. In Go, "declared" always means "safely usable."
Multiple declarations at once
var x, y int = 10, 20
name, age := "Ada", 32This is handy for grouping related values, and it's the mechanism Go uses to return multiple values from a function — covered in a later lesson.
Naming and visibility
Go doesn't have public/private keywords. Instead, capitalization controls visibility:
var Exported = "visible outside this package"
var unexported = "only visible within this package"A name starting with an uppercase letter is exported (accessible from other packages that import this one); lowercase is package-private. This applies to variables, functions, types, and struct fields alike — it's one of the first Go idioms that feels strange coming from other languages, and one you'll use constantly.
Constants, briefly
const declares values that can't change and must be known at compile time:
const MaxUsers = 100The next lesson covers constants and Go's distinctive iota mechanism for generating related constant values in detail.
Test what you just learned
4 quick questions. Get all of them right to unlock the next lesson.
You can take the quiz without an account — logging in just lets your result count toward your progress.