Start learning free

LESSON 4 OF 338 · WELCOME TO GO

Variables in Go: var and the Short Declaration

Lesson Read the full explanation

TL ; DR:

Variables are containers for storing values.

In Go, you can declare a variable with var:

var car string = "Toyota"
var experience int = 3

Inside a function, Go also lets you use short declaration with :=:

car := "Toyota"
experience := 3


Full lesson:

A piece of information lives in memory, and we use a variable to store and label that data.

A variable declaration can include:

  • a name, such as name
  • a type, such as string or int
  • a value, such as "AlgoCademy"
var name string = "AlgoCademy"
var age int = 10

fmt.Println(name)
fmt.Println(age)

The output is:

AlgoCademy
10

Assignment
Follow the Coding Tutorial and let's create some variables.


Hint
Use var for the typed declarations and := for the short declaration step.


STEP 1 OF 3
    Ctrl + Enter to check

    GO · WELCOME TO GO

    Variables in Go, explained

    What a variable is

    Every piece of information a program works with lives in memory. A variable gives one such piece a name, so you can store it, read it back and change it later. Without variables, every value would have to be written out again each time you needed it.

    A Go variable has three parts: a name such as name, a type such as string or int, and a value such as "AlgoCademy". The type is fixed when the variable is declared and cannot change afterwards.

    Syntax and examples

    Declaring with var

    The full form is the keyword var, the name, the type and the value. In Go the type comes after the name:

    var name string = "AlgoCademy"
    var age int = 10
    fmt.Println(name)
    fmt.Println(age)
    
    AlgoCademy
    10
    

    Text values go in double quotes. Numbers have no quotes: 31 is a number, "31" is a string.

    The short declaration :=

    Inside a function, you can leave out both var and the type. The operator := declares the variable and lets Go infer the type from the value:

    car := "Toyota"
    experience := 3
    fmt.Printf("%T %T\n", car, experience)
    
    string int
    

    The format verb %T prints a value's type. A whole number like 3 becomes an int, a number with a decimal point like 3.5 becomes a float64, and text becomes a string. The short form is what most Go code uses inside functions.

    Changing a value

    After a variable exists, assign a new value with plain =. The new value must have the same type:

    age := 31
    age = 32
    fmt.Println(age)
    
    32
    

    Default values

    A variable declared with var and no value starts at its type's zero value: 0 for numbers, "" for strings and false for booleans:

    var count int
    var label string
    var ok bool
    fmt.Printf("%d %q %t\n", count, label, ok)
    
    0 "" false
    

    Several variables at once

    One := can create several variables. The values on the right are assigned to the names on the left, in order:

    c, d := 2, 3.5
    fmt.Println(c, d)
    
    2 3.5
    

    Here c is an int and d is a float64. If at least one name on the left is new, := is allowed, and it just assigns to the names that already exist. That rule shows up often when functions return more than one value.

    Common mistakes and gotchas

    • Declaring the same name twice with :=. After x := 1, writing x := 2 in the same scope fails with no new variables on left side of :=. Use x = 2 to change it.
    • Using = before declaring. A plain assignment only works on a variable that already exists.
    • Putting := outside a function. At the top level of a file, x := 5 is a syntax error (non-declaration statement outside function body). Use var x = 5 there.
    • Wrong value for the type. var age int = "31" fails with cannot use "31" (untyped string constant) as int value. The same happens when you assign text to an int variable later.
    • Unused variables. Go refuses to compile a program with a local variable that is declared and never used (declared and not used). Use it, for example by printing it, or remove it.
    • Type after the name. int age is wrong. Go writes age int.

    The exercise

    The lesson asks for three variables on three lines.

    1. Declare name with var, type string, value "Michael".
    2. Declare age with var, type int, value 31. It is a number, so it has no quotes.
    3. Declare language with := and the value "Go". Go infers the type string from the value, so you don't write it.
    var name string = "Michael"
    var age int = 31
    language := "Go"
    

    The first two lines state the type explicitly, and the third lets Go work it out. All three do the same job of creating a named value. The variables are only declared, not printed; printing them is one fmt.Println call each, as shown above.

    Key takeaways

    • A variable has a name, a type and a value; the type is fixed once declared.
    • var name type = value is the full form, and the type comes after the name.
    • name := value declares a variable inside a function and infers its type.
    • Use = to change an existing variable and := only to create a new one.
    • Go rejects unused variables and values of the wrong type at compile time.
    Keep going with GoAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free