19

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.

moodymudskipper
  • 46,417
  • 11
  • 121
  • 167
sergserg
  • 21,716
  • 41
  • 129
  • 182
  • 1
    @BenjaminGruenbaum I guess it's a case of knowing what to search for. I tried `go := meaning`, `golang :=` and nothing relevant came up. – sergserg May 16 '13 at 01:29
  • For what it's worth, it wasn't where I usually looked for Go resources/specification – Benjamin Gruenbaum May 16 '13 at 01:30
  • 7
    If you run into something in the language that you don't understand, instead of googling (which is definitely not going to work well for punctuation), just pop open [golang.org/ref/spec](http://golang.org/ref/spec) and search there. That's the actual spec for the language, and it's not very large. Second occurrence of ":=" on the page is precisely what you want. – Lily Ballard May 16 '13 at 01:39
  • 2
    It's explained [in the tutorial](http://tour.golang.org/#12) –  May 16 '13 at 01:50
  • I came here from google, so the question might not be entirely invalid. – Doug Molineux Nov 07 '15 at 17:53
  • It's just a shorthand for declaring and initializing a variable (or multiple variables when using a comma). The types are inferred from the values passed in. So in your case, you would have two variables `a` and `b`, both of type `string`, initialized with the values returned from `swap("hello", "world")` – pjmonk Jun 02 '20 at 19:04

7 Answers7

21

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:

NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143
Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
8

Keep on going to page 12 of the tour!

A Tour of Go

Short variable declarations

Inside a function, the := short assignment statement can be used in place of a var declaration with implicit type.

(Outside a function, every construct begins with a keyword and the := construct is not available.)

peterSO
  • 158,998
  • 31
  • 281
  • 276
7

As others have explained already, := is for both declaration, and assignment, whereas = is for assignment only.

For example, var abc int = 20 is the same as abc := 20.

It's useful when you don't want to fill up your code with type or struct declarations.

javeria
  • 81
  • 2
  • 9
5

The := syntax is shorthand for declaring and initializing a variable, example f := "car" is the short form of var f string = "car"

The short variable declaration operator(:=) can only be used for declaring local variables. If you try to declare a global variable using the short declaration operator, you will get an error.

Refer official documentation for more details

MeanwhileInHell
  • 6,780
  • 17
  • 57
  • 106
Arun
  • 1,651
  • 4
  • 20
  • 31
1

:= is not an operator. It is a part of the syntax of the Short variable declarations clause.

more on this: https://golang.org/ref/spec#Short_variable_declarations

0

According to my book on Go, it is just a short variable declaration statement exactly the same as

var s = ""

But it is more easy to declare, and the scope of it is less expansive. A := var decleration also can't have a type of interface{}. This is something that you will probably run into years later though

liam
  • 341
  • 2
  • 5
  • 9
  • The only difference between the two is that var can be used in a global scope. You can also use `var` in functions, and it'll behave exactly the same as `:=`. Also, you can use the `interface{}` type with `var` declarations. – pjmonk Jun 02 '20 at 19:08
-2

:= represents a variable, we can assign a value to a variable using :=.