var vs := in Go

落花浮王杯 提交于 2019-12-17 19:33:13

问题


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.


回答1:


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.




回答2:


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
}



回答3:


The Go Programming Language Specification

Short variable declarations

A short variable declaration uses the syntax:

ShortVarDecl = IdentifierList ":=" ExpressionList .

Short variable declarations may appear only inside functions.

In your example, changing the variable declaration statement outside a function body

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

to a short variable declaration statement outside a function body

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

fails with compiler error "non-declaration statement outside function body."



来源:https://stackoverflow.com/questions/21657446/var-vs-in-go

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!