var vs :=

2,113 views
Skip to first unread message

ikarus

unread,
Sep 9, 2011, 10:52:09 PM9/9/11
to golang-nuts
Hey I'm new to Go and have been watching a lot of videos and reading
things, I just have a quickie question here for you. I've seen people
using both "var x = 2" and "x := 2". So what's the exact difference
between assigning either way?

David Symonds

unread,
Sep 10, 2011, 12:18:08 AM9/10/11
to ikarus, golang-nuts

Both of those are declarations + assignments, not just an assignment.
There's no difference between them, except that the former may be used
at package level (i.e. outside a function), but the latter may not.

The other variation is "var x int = 2", which includes the type
information. "var x = 2" and "x := 2" infer the variable type from the
right-hand side, but "var x int = 2" explicitly sets it. You might
want to do that if, for instance, you want the variable to be typed
something different than what would be inferred, such as in "var x
int64 = 2", or "var r io.Reader = bytes.NewBuffer(foo)".


Dave.

unread,
Sep 10, 2011, 3:04:53 AM9/10/11
to golang-nuts
There is no difference between "var x = 2" and "x := 2". This is
because both are using just 1 variable. The difference between "var"
and ":=" begins with 2 or more variables.

"var x, y = 2, 3" is slightly safer to use than "x, y := 2, 3". The
former ensures that both "x" and "y" do not already exist in the
*current* scope.

Error handling in Go usually uses the ":=" version:

result1, err := Function1()
if err != nil { ... }
...
result2, err := Function2()
if err != nil { ... }

peterGo

unread,
Sep 10, 2011, 3:59:36 AM9/10/11
to golang-nuts
ikarus,

"var x = 2" is a variable declaration and "x := 2" is a short
variable declaration. A short variable declaration is a shorthand for
a regular variable declaration with initializer expressions but no
types.

Variable declarations
http://golang.org/doc/go_spec.html#Variable_declarations

Short variable declarations
http://golang.org/doc/go_spec.html#Short_variable_declarations

Peter

unread,
Sep 10, 2011, 6:36:34 AM9/10/11
to golang-nuts
On Sep 10, 9:59 am, peterGo <go.peter...@gmail.com> wrote:
> ikarus,
>
> "var x = 2" is a variable declaration and  "x := 2" is a short
> variable declaration. A short variable declaration is a shorthand for
> a regular variable declaration with initializer expressions but no
> types.

If a programming language construct A is shorthand for B, it means
that A and B are equivalent (except for their appearance/syntax).

The original question was: "So what's the exact *difference* between
assigning either way?"

The original question was about the differences - not about the
equivalences.

> Variable declarationshttp://golang.org/doc/go_spec.html#Variable_declarations
>
> Short variable declarationshttp://golang.org/doc/go_spec.html#Short_variable_declarations

Rich Wareham

unread,
Sep 10, 2011, 7:12:37 AM9/10/11
to ⚛, golang-nuts
On Sat, Sep 10, 2011 at 03:36:34AM -0700, ⚛ wrote:
> On Sep 10, 9:59 am, peterGo <go.peter...@gmail.com> wrote:
> > ikarus,
> >
> > "var x = 2" is a variable declaration and  "x := 2" is a short
> > variable declaration. A short variable declaration is a shorthand for
> > a regular variable declaration with initializer expressions but no
> > types.
>
> If a programming language construct A is shorthand for B, it means
> that A and B are equivalent (except for their appearance/syntax).
>
> The original question was: "So what's the exact *difference* between
> assigning either way?"
>
> The original question was about the differences - not about the
> equivalences.

I imagine that's why the differences were stated: one is short form, one
is long form. One has an optional initialiser, one doesn't.

If you're going to troll, at least get it right :).

--
Dr Rich Wareham

unread,
Sep 10, 2011, 8:07:54 AM9/10/11
to golang-nuts
On Sep 10, 1:12 pm, Rich Wareham <rj...@cam.ac.uk> wrote:
> If you're going to troll, at least get it right :).

No comment.

ikarus

unread,
Sep 10, 2011, 1:41:19 PM9/10/11
to golang-nuts
Current scope? How does the scope work in Go? I come from a mostly
javascript background where you have to watch yourself when making
variables so they don't go global, are the scopes better in go where
it's automatically local inside a function?

ikarus

unread,
Sep 10, 2011, 1:44:16 PM9/10/11
to golang-nuts
> If you're going to troll, at least get it right :).

Haha, I love how much internet communication has gotten so prevalent
that "Dr"s have to worry about trolling XD

Steven Blenkinsop

unread,
Sep 10, 2011, 1:53:02 PM9/10/11
to ikarus, golang-nuts
On Sat, Sep 10, 2011 at 1:41 PM, ikarus <iris....@gmail.com> wrote:
Current scope? How does the scope work in Go? I come from a mostly
javascript background where you have to watch yourself when making
variables so they don't go global, are the scopes better in go where
it's automatically local inside a function?

Yes. I would recommend reading this document:

Relevant here is:

Kyle Lemons

unread,
Sep 10, 2011, 1:56:57 PM9/10/11
to ikarus, golang-nuts
On Sat, Sep 10, 2011 at 10:41 AM, ikarus <iris....@gmail.com> wrote:
Current scope? How does the scope work in Go? I come from a mostly
javascript background where you have to watch yourself when making
variables so they don't go global, are the scopes better in go where
it's automatically local inside a function?

Go variable scoping is basically the same as other C-like languages.  The thing about := though, is that it doesn't always define a new variable for everything on the LHS, if one of the LHS is already defined in the same (e.g. not parent) scope.

// Declare foo and err
foo, err := bar()
if err != nil { ... } 

// Declare spam
spam, err := eggs()
if err != nil { ... }

if foo || spam {
  // Declare forty and err
  forty, err := two()
  if err != nil { ... }

  // Declare blah and err in if-scope
  if blah, err := boring(); err != nil { ... }
}

Andrew Gerrand

unread,
Sep 11, 2011, 8:32:44 PM9/11/11
to golan...@googlegroups.com
On Sunday, September 11, 2011 3:41:19 AM UTC+10, ikarus wrote:
Current scope? How does the scope work in Go? I come from a mostly
javascript background where you have to watch yourself when making
variables so they don't go global, are the scopes better in go where
it's automatically local inside a function?
 
In Go it's impossible to declare a name in a scope "above" the current scope. You don't have to worry about that JavaScript "oops I forgot the var" issue.

Scope in Go is very regular. The precise definitions of scope are stated clearly in the spec. You cannot declare a name twice in the same scope, but you _can_ declare (and therefore shadow) a name that was defined in one of the scope's ancestors.

The caveat with := that atom-symbol-unicode-guy mentioned is that := lets you re-use names declared in the current scope, as long as one of the names in the declaration is new to the current scope. Sounds confusing at first, but it becomes clear in practice.

I'm working on a blog post to explain this in more detail.

Andrew


Andrew Chilton

unread,
Sep 11, 2011, 9:52:17 PM9/11/11
to golan...@googlegroups.com
On 12 September 2011 12:32, Andrew Gerrand <a...@golang.org> wrote:
> [snip]

> Scope in Go is very regular. The precise definitions of scope are stated
> clearly in the spec. You cannot declare a name twice in the same scope, but
> you _can_ declare (and therefore shadow) a name that was defined in one of
> the scope's ancestors.
> [snip]

Just an "if you're interested" story. I noticed this the other day and
had to read around the spec to see what was going on. At first I
couldn't figure it out but later found this, for those that missed it:

* http://golang.org/doc/go_spec.html#Declarations_and_scope

"An identifier declared in a block may be redeclared in an inner
block. While the identifier of the inner declaration is in scope, it
denotes the entity declared by the inner declaration."

In my example, I had a var named 'url' (under r59) but with r60 gofix
renamed it to 'url_' so that was good. When playing I renamed it back
to 'url' and wondered whether it would conflict with the package name.
Everything still worked so this was an instance where the local 'url'
variable shadowed the imported package 'url'. To finish though, I
ended up naming my 'url' var to something completely different and
more useful.

Cheers,
Andy

--
Andrew Chilton
e: chi...@appsattic.com
w: http://www.appsattic.com/

Reply all
Reply to author
Forward
0 new messages