What does := mean in Go?
Asked Answered
S

7

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.

Stickle answered 16/5, 2013 at 1:27 Comment(6)
@BenjaminGruenbaum I guess it's a case of knowing what to search for. I tried go := meaning, golang := and nothing relevant came up.Stickle
For what it's worth, it wasn't where I usually looked for Go resources/specificationTerpsichore
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 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.Spermatozoid
It's explained in the tutorialBlend
I came here from google, so the question might not be entirely invalid.Crossover
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")Nkrumah
T
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:

Terpsichore answered 16/5, 2013 at 1:30 Comment(0)
A
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.)

Amundsen answered 16/5, 2013 at 1:47 Comment(0)
B
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.

Bath answered 12/2, 2021 at 16:50 Comment(1)
Take my comment as a little joke: no aswer should start with "As others have explained already...", right? 😉Espalier
M
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

Magness answered 10/2, 2021 at 15:57 Comment(3)
You could add a link to the official doc, like the accepted answer. I also think that this symbol can only be used in local variables, right? If that's true, you could add that.Espalier
@FerranMaylinch Thanks for the suggestions to make it more accurate ..Magness
It's funny that you got a negative vote, and then someone wrote the same answer 😁Espalier
R
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

Rework answered 13/11, 2018 at 13:21 Comment(0)
T
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

Terrapin answered 25/11, 2015 at 14:53 Comment(1)
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.Nkrumah
B
-2

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

Bulldoze answered 23/11, 2015 at 8:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.