Basic

Variables and Constants

Declare and assign values using short declarations and the var keyword.

What you will see

Ada is 30 Pi is 3.14159

About 2 min of reading

Quick check

Before this new lesson, one small question about the lesson you just finished. Recall it, do not look it up.

Try It

Go decides a variable's type from the value on the right of :=, and every variable always has a zero value. You will declare variables a few different ways and read the results back from the screen.

1

I use := to declare and initialize four variables in one step. Go infers the type of each from the literal: a string, an int, a float, and a bool. I print them to see each value on one line.

package main import "fmt" func main() { name := "Ada" age := 30 height := 1.75 isCool := true fmt.Println(name, age, height, isCool) }

Before you run it, say out loud what it will print. Then open the answer.

What it prints
Ada 30 1.75 true
:= does short variable declaration: it both names the variable and infers its type from the value, so you never write the type yourself here.
2

Now I use the var keyword for a more explicit style. var name string declares an empty string, while var age = 30 still infers the type. I print the string wrapped in brackets so an empty value is clearly visible.

package main import "fmt" func main() { var name string var age = 30 fmt.Println("["+name+"]", age) }

Before you run it, say out loud what it will print. Then open the answer.

What it prints
[] 30
var name string gives name its zero value, which for a string is the empty string. That is why the brackets print nothing inside them.
3

I let Go fill in zero values for me. I declare several variables without assigning them and print each one at a time: the int, the float, the string, and the bool all show their built-in defaults.

package main import "fmt" func main() { var empty int var emptyFloat float64 var emptyString string var emptyBool bool fmt.Println("["+emptyString+"]", empty, emptyFloat, emptyBool) }

Before you run it, say out loud what it will print. Then open the answer.

What it prints
[] 0 0 false
Every Go variable has a fixed zero value per type: int is 0, float64 is 0, string is "", and bool is false. A declared-but-unassigned variable is already safe to read.
4

I add constants with the const keyword. A constant like Pi or greeting is fixed at compile time and cannot change afterward. I print both to confirm constants are read just like variables.

package main import "fmt" const Pi = 3.14159 const greeting = "Hello" func main() { fmt.Println(greeting, Pi) }

Before you run it, say out loud what it will print. Then open the answer.

What it prints
Hello 3.14159
const must be declared at package scope, not inside main, so the values above main() come before the function body.
5

I mix everything together: a package-level constant Pi plus two variables, printed in one statement with words between them. Reading this line ties the whole lesson together.

package main import "fmt" const Pi = 3.14159 func main() { name := "Ada" age := 30 fmt.Println(name, "is", age, "Pi is", Pi) }

Before you run it, say out loud what it will print. Then open the answer.

What it prints
Ada is 30 Pi is 3.14159
Go is strictly typed: do not try to combine an int and a float in one expression without converting with float64() or int() first.

Put it in order

Before you write anything, put these lines back in the right order. Click a line to move it up one step. This support step is easier than writing code from scratch, and it is here to make the last example stick.

Data Flow

A literal value is assigned; the compiler infers its type and stores it in a named slot.
1 Write a literal on the right of :=
↓
2 The compiler infers the type from the literal
↓
3 A variable of that type is allocated
↓
4 The value is stored and can be read later
Why this works
Zero values

Every variable in Go has a type, and every type has a zero value.

TypeZero value
int0
float640
boolfalse
string""
pointernil

If you declare var age int without assigning, age is already 0 and safe to use.

Type inference

When you use :=, Go works out the type from the literal:

name := "Ada"   // inferred as string
age := 30       // inferred as int
pi := 3.14      // inferred as float64

This is called type inference. It keeps code short but means you should pick literal types carefully, 30 is an int, not a float.

Examples

Short declaration with :=

Declaring variables

The := operator declares and initializes a variable in one step:

name := "Ada"
age := 30
height := 1.75
isCool := true

Go infers each type from the value on the right: string, int, float64, bool.

Why it works: := is short variable declaration, the compiler determines the type, so you never write it twice.

The var keyword

Explicit declaration

var name string
var age = 30
var height float64 = 1.75

var name string declares an empty string. var age = 30 infers the type. You can group several:

var (
    name  = "Ada"
    age   = 30
)

Unassigned variables become their zero value: 0, "", false, or nil.

Constants with const

Fixed values

const Pi = 3.14159
const greeting = "Hello"

const values cannot change after declaration. The compiler replaces them at build time. Use constants for things like configuration, units, or flags that never vary.

Words this lesson uses

Each one is defined here on this site. Open the entry for the full definition and the other words it points at.

← Your First Go Program
Previous Lesson
Your First Go Program
Next Lesson
Primitive Types
Primitive Types →