FAQ
Eliminate ':=' and require all variable declarations with var:
* No ':='
* declaration always start with var
* in multiple declaring (and assignment), each variable that need to
declare must start with var

So something like this;

var v = 10;
var s, var err = some_function() //
var t, err = another_function()
var i, var j int

Unfortunately, this does not compatible with current go syntax. It means
that any code declared with 'var i, j int' must convert to 'var i, var j
int'. So this will never happen :(

On Tuesday, May 6, 2014 8:45:53 AM UTC-7, Joshua Rubin wrote:

Short declarations is a stupid feature that adds more complexity to the
language and should be removed. But if someone tried to improve golang we
could have the flame bait raging like there was with igo.
I also heard Rob state his issues with variable declaration and shadowed
variables biting too many people. Yet, I am not sure how things could be
improved much.

There is currently a way to declare a variable with explicit type (using
var).
There is also a way to declare a variable with implicit type and
initialization (using :=).

I agree with the OP that it is odd that := is allowed in multiple variable
initialization even if not all variables are new. At least you can be
assured (in the OP example) of the type and scope of 'err' (especially in a
nested scope where foo, err := bar() might actually shadow an 'err'
declared in an outer scope).

How could this be improved?

* Make ':=' implied on the first occurrence of a variable (essentially
allowing '=' to declare a new, initialized variable with implied type)?
This would likely introduce accidental scoping problems similar to
JavaScript.

* Eliminate ':=' and require all variable declarations with var?
I think not having to determine type all the time is a great time saver
and fail to see how this would improve the language.

* Eliminate shadowed variables so that any variable declaration (either
through 'var' or ':=') of a variable whose name is already in scope will
cause a compiler error.
OK, there might be a case for this. I understand shadowing has idiomatic
uses, but it can be very confusing.

But what is the point of discussing this anyway?
http://golang.org/doc/go1compat

My point is that what we have now isn't SO bad.
--
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/d/optout.

Search Discussions

Discussion Posts

Previous

Follow ups

Related Discussions

People

Translate

site design / logo © 2021 Grokbase