Skip to content

Go Variables

A variable is a named storage location in memory that holds a value. Programs use variables to store and manipulate data.

Every variable in Go has a specific type, which dictates:

Variable names (identifiers) follow the rules outlined in the Basic Syntax chapter (start with letter or underscore, followed by letters, digits, underscores; case-sensitive).

Go offers a rich set of built-in types. Here are some fundamental ones:

TypeDescriptionExample Values
boolBoolean valuetrue, false
stringSequence of bytes (often representing UTF-8 encoded text)"Hello, Go!", ""
intSigned integer (size depends on the platform: 32 or 64 bits)-10, 0, 123
int8, int16, int32, int64Signed integers with specific bit sizes-128 to 127 (int8), etc.
uintUnsigned integer (size depends on the platform: 32 or 64 bits)0, 42, 1000
uint8, uint16, uint32, uint64Unsigned integers with specific bit sizes0 to 255 (uint8), etc.
byteAlias for uint8'a', 0x41
runeAlias for int32. Represents a Unicode code point.'✓', '世'
float32, float64Floating-point numbers (IEEE-754 standard). float64 is generally preferred.3.14, -0.5, 1.0e6
complex64, complex128Complex numbers (real and imaginary parts are float32 or float64)complex(2, -3)

Go also provides more complex types like arrays, slices, maps, structs, pointers, channels, interfaces, and functions, which are covered in later chapters.

Declaring a variable tells the compiler to reserve memory for it. Go offers several ways to declare variables:

  1. Declare with var and type (Zero Value):

This form declares one or more variables of a specified type. If no initial value is provided, the variable is initialized to its zero value (e.g., 0 for numeric types, false for bool, "" for string, nil for pointers, slices, maps, etc.).

var count int // Declares an int variable named 'count', initialized to 0
var message string // Declares a string variable 'message', initialized to ""
var isActive bool // Declares a bool variable 'isActive', initialized to false
var i, j, k int // Declares multiple int variables, all initialized to 0
  1. Declare with var, type, and initial value:

This form declares a variable and explicitly assigns an initial value.

var score int = 100
var name string = "Alice"
var pi float64 = 3.14159
  1. Declare with var and initial value (Type Inference):

If you provide an initial value, Go can often infer the type, so you can omit the type specification.

var age = 30 // Type inferred as int
var greeting = "Hi there" // Type inferred as string
var enabled = true // Type inferred as bool
// Declare multiple variables with inferred types
var a, b, c = 1, 2.5, "xyz"
  1. Short Variable Declaration (:=) (Type Inference):

Inside functions, you can use the := short assignment statement. This declares the variable(s) and initializes them using type inference. This is the most common way to declare variables within functions in Go.

Note: Short variable declarations (:=) can only be used inside functions, not at the package level. At the package level, you must use var.

func main() {
count := 10 // Type inferred as int
message := "Success" // Type inferred as string
isValid := false // Type inferred as bool
x, y := 5, "five" // Declare multiple variables
}

When to use var vs :=:

package main
import "fmt"
// Package-level variable (must use var)
var globalMessage = "This is global"
func main() {
// Using var with zero value
var counter int
fmt.Printf("Initial counter: %d (Type: %T)\n", counter, counter)
// Using var with explicit type and value
var price float64 = 99.95
fmt.Printf("Price: %.2f (Type: %T)\n", price, price)
// Using var with type inference
var quantity = 5
fmt.Printf("Quantity: %d (Type: %T)\n", quantity, quantity)
// Using short variable declaration (:=)
productName := "Widget"
fmt.Printf("Product: %s (Type: %T)\n", productName, productName)
// Mixed declaration with type inference
a, b, c := true, 10.5, "Mixed"
fmt.Printf("a: %t (%T), b: %.1f (%T), c: %s (%T)\n", a, a, b, b, c, c)
fmt.Println(globalMessage)
}

When compiled and executed, this produces:

Initial counter: 0 (Type: int)
Price: 99.95 (Type: float64)
Quantity: 5 (Type: int)
Product: Widget (Type: string)
a: true (bool), b: 10.5 (float64), c: Mixed (string)
This is global

In assignment operations (=, :=), the left side must refer to a memory location where a value can be stored (sometimes called an lvalue or, more simply, an assignable operand). The right side must produce a value (sometimes called an rvalue).

Variables are assignable:

count = 10 // Valid: 'count' refers to a memory location

Literals (like numbers or strings) are values, not memory locations, so they cannot be on the left side of an assignment:

100 = count // Invalid: Cannot assign to the literal value 100