What does := mean in Go?

sergserg picture sergserg · May 16, 2013 · Viewed 10k times · Source

I'm following this tutorial, specifically exercise 8:

http://tour.golang.org/#8


package main

import "fmt"

func swap(x, y string) (string, string) {
    return y, x
}

func main() {
    a, b := swap("hello", "world")
    fmt.Println(a, b)
}

Specifically what does the := mean? Searching for Go documentation is very hard, ironically.

Answer

Benjamin Gruenbaum picture Benjamin Gruenbaum · May 16, 2013

A short variable declaration uses the syntax:

ShortVarDecl = IdentifierList ":=" ExpressionList .

It is a shorthand for a regular variable declaration with initializer expressions but no types: