Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

var vs := in Go

In the Go web server example here: http://golang.org/doc/effective_go.html#web_server

The following line of code works

var addr = flag.String("addr", ":1718", "http service address") 

but changing it to

addr := flag.String("addr", ":1718", "http service address") 

is a compilation error. Why? Does it have anything to do with the face that the return type of the function is *string instead of string? What difference does that make?

UPDATE: Thanks for pointing out that := is not allowed at the top level. Any idea why this inconsistency is in the spec? I don't see any reason for the behaviour to be different inside a block.

like image 445
Sudhir Jonathan Avatar asked Feb 09 '14 09:02

Sudhir Jonathan


People also ask

What is difference between and := IN go?

In Go, := is for declaration + assignment, whereas = is for assignment only. For example, var foo int = 10 is the same as foo := 10 . @KennyWorden, yes.

Is it true that short variable declaration := can be used only inside a function?

Inside a function, the := short assignment statement can be used in place of a var declaration with implicit type. Outside a function, every statement begins with a keyword ( var , func , and so on) and so the := construct is not available.

What is var _ Golang?

Posted on June 21, 2020. var keyword is a reserved keyword in golang which is used to declare variables in go .variables are declared using the var keyword but there are other ways of declaring a variable too such as using := operator.

Should I use var?

var can even hold various objects and will behave properly. As you probably already know, C# has supported the variable type var since version 3.0. Ever since, the debate has raged on: you should always use var; you should never use var.


2 Answers

In Go, top-level variable assignments must be prefixed with the var keyword. Omitting the var keyword is only allowed within blocks.

package main  var toplevel = "Hello world"         // var keyword is required  func F() {         withinBlock := "Hello world" // var keyword is not required } 
like image 89
publysher Avatar answered Sep 20 '22 04:09

publysher


On the updated question: there is actually a difference between long and short declarations, being in that short form allows redeclaration of variables.

From spec:

Unlike regular variable declarations, a short variable declaration may redeclare variables provided they were originally declared earlier in the same block with the same type, and at least one of the non-blank variables is new. As a consequence, redeclaration can only appear in a multi-variable short declaration. Redeclaration does not introduce a new variable; it just assigns a new value to the original.

field1, offset := nextField(str, 0) field2, offset := nextField(str, offset)  // redeclares offset a, a := 1, 2                              // illegal: double declaration of a or no new variable if a was declared elsewhere 

So I'd say the := operator is not pure declare, but more like declare and assign. Redeclaration in toplevel is not allowed, so neither are short declarations.

Another reason for this might be syntax simplicity. In Go all toplevel forms start with either type, var or func. Short declarations there will ruin all the cuteness.

like image 38
Mikola Avatar answered Sep 19 '22 04:09

Mikola