Skip to main content
You cannot re-declare a variable that has already been declared in the same scope.

Using the var keyword

var varName type = value
Variables declared without an explicit initial value are given their zero value.

Basic Declaration

You must specify either type or value (or both):
var name string = "John"  // explicit type and value
var surname = "Doe"       // type is inferred as string
var age int16             // explicit type, initialized to the zero value (0)

Multiple Variables

Declaring multiple variables of the same or different types on a single line:
var name, age = "John", 30  // mixed types, types inferred
var a, b, c int = 1, 2, 3   // all must be of the declared type
var x, y int                // both initialized to the zero value (0)

Grouped Declaration

Using grouped declaration syntax to declare variables together:
var (
	name    string = "John"
	surname        = "Doe"  // type is inferred as string
	age     int             // zero value (0)
)

Using the := syntax

varName := value
Can only be used inside functions, not at the package level.

Short Declaration

Declaring multiple variables on a single line:
name, age := "John", 30

Using the const keyword

Constants are fixed values that cannot be changed after they are declared. They are read-only.
const varName type = value

Basic Constants

Constants can be declared without explicit types, but they must have values:
const daysInWeek int = 7  // explicit type and value
const hoursInDay = 24     // type is inferred as int

Grouped Constants

Declaring multiple constants in a single block:
const (
	daysInWeek = 7
	hoursInDay = 24
)
Computations on constants are mostly done at compile time, not at runtime.

Build docs developers (and LLMs) love