Skip to main content

Introduction

In Go, variables are explicitly declared and the compiler uses them to check type-correctness of function calls. Go provides multiple ways to declare variables, from explicit declarations to type inference and shorthand syntax.

The Code

// In Go, _variables_ are explicitly declared and used by
// the compiler to e.g. check type-correctness of function
// calls.

package main

import "fmt"

func main() {

	// `var` declares 1 or more variables.
	var a = "initial"
	fmt.Println(a)

	// You can declare multiple variables at once.
	var b, c int = 1, 2
	fmt.Println(b, c)

	// Go will infer the type of initialized variables.
	var d = true
	fmt.Println(d)

	// Variables declared without a corresponding
	// initialization are _zero-valued_. For example, the
	// zero value for an `int` is `0`.
	var e int
	fmt.Println(e)

	// The `:=` syntax is shorthand for declaring and
	// initializing a variable, e.g. for
	// `var f string = "apple"` in this case.
	// This syntax is only available inside functions.
	f := "apple"
	fmt.Println(f)
}

Variable Declaration Methods

Basic Declaration with Initialization

var a = "initial"
The var keyword declares a variable. When you provide an initial value, Go infers the type automatically.

Multiple Variable Declaration

var b, c int = 1, 2
You can declare multiple variables of the same type in a single statement. Here we explicitly specify the int type.

Type Inference

var d = true
Go automatically infers that d is a bool based on the initial value true.
Type inference makes code cleaner while maintaining type safety. The compiler determines the type at compile time.

Zero Values

var e int
fmt.Println(e)  // prints: 0
Variables declared without initialization are given their “zero value”:
  • 0 for numeric types
  • false for booleans
  • "" (empty string) for strings
  • nil for pointers, slices, maps, channels, functions, and interfaces
There’s no “undefined” or “null” in Go. Every variable always has a value, even if you don’t explicitly initialize it.

Short Declaration (:=)

f := "apple"
The := syntax is shorthand for declaring and initializing a variable. This is equivalent to var f string = "apple" but more concise.
The := syntax is only available inside functions. At package level, you must use the var keyword.

Declaration Styles Comparison

StyleExampleWhen to Use
var with type and valuevar x int = 10When you want to be explicit
var with inferencevar x = 10When type is obvious from value
var without valuevar x intWhen you want zero value
Short declarationx := 10Most common inside functions

Key Takeaways

  • Use var for explicit variable declarations
  • Go infers types when you provide initial values
  • Uninitialized variables get their type’s zero value
  • The := shorthand is convenient for function-local variables
  • := can only be used inside functions, not at package level
  • Variables in Go are always typed - no dynamic typing

Build docs developers (and LLMs) love