FAQ
If I declare foo := 42 and then later try to declare foo := 69, I get an
error "no new variables on left side of :=". If however I declare foo := 42
and then later try to declare foo, err := bar(), where bar() is a function
that returns two values, it doesn't complain, and reassigns foo to whatever
value bar returned. Am I correct in assuming that := acts identically to =
for the sake of convenience in this case, or is there some semantic
difference?

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Andrey mirtchovski at Nov 22, 2013 at 7:43 am
    see the last two paragraphs of
    http://golang.org/ref/spec#Short_variable_declarations

    On Fri, Nov 22, 2013 at 12:41 AM, wrote:

    If I declare foo := 42 and then later try to declare foo := 69, I get an
    error "no new variables on left side of :=". If however I declare foo := 42
    and then later try to declare foo, err := bar(), where bar() is a function
    that returns two values, it doesn't complain, and reassigns foo to whatever
    value bar returned. Am I correct in assuming that := acts identically to =
    for the sake of convenience in this case, or is there some semantic
    difference?

    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Alex Skinner at Nov 22, 2013 at 8:33 am
    := is not the same as =, and I was once bit by a bug in my code on the assumption it was for existing variables. If you declare/define a variable at say, the global scope, any assignment with = assigns here. However, reassigning a variable with :=, say, in an if block only defines it in that scope, and does not affect the global variable.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedNov 22, '13 at 7:41a
activeNov 22, '13 at 8:33a
posts3
users3
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase