All lessons

Variables

7 min · Go

Store values in named boxes called variables.

Go · Variables0/5 cleared

Enemy 1 of 5

Short variable declaration with :=

Go still gives every variable a fixed type — it's a statically typed language, just like C, C++, and C# — but it usually lets you skip writing the type out yourself. name := "Ada" creates a new variable called name and, because you're assigning it a string right away, Go infers (figures out) that name's type is string, without you spelling out "string" anywhere.

The := operator (called short variable declaration) is one of Go's most distinctive pieces of syntax: it's only used the first time you create a variable. To change an existing variable's value afterward, you use a plain = instead — writing := again for a variable that already exists is actually a compile error in many contexts, a common early stumble for newcomers.

This type inference isn't the same thing as dynamic typing in Python or JavaScript — Go still locks in one specific type for name forever, at compile time; it just saves you from typing that type explicitly when it's obvious from context.

You
Cold-Start Frostling

Loading editor...

Ln 1, Col 1Spaces: 4
Sign up to fightTarget output: Ada