--
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...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
No, the reason for short variable declarations is to avoid having to stutter the type of variables everywhere.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
Hi TL,I can't talk on behalf of the creators of the language; but from my personal experience; it makes code more succinct and easier to write; something more or less like "doing more with less" if you know what I mean. It's one the the features I love the most in Go. It just makes programming more interesting. Rather spending much time writing a lot of code which I think doesn't make much sense in today's programming world where the programmer's time is very expensive compared to hardware. Le's just say it reduces the number of lines immensely in code.
sorry, from my experience, short form doesn't reduces the number of lines immensely comparing to var form.
And sometimes, more code lines are needed by using short form.
--
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.
var int i = 0
This can still workvar int i = 0for i < MAX {//statements herei++}Cheers!
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
-j
--
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.
Interesting. I have never used var this way. I am the student here. (as in everything else!)
From: Axel Wagner <axel.wa...@googlemail.com>
Date: Wednesday, October 19, 2016 at 2:39 PM
To: Michael Jones <michae...@gmail.com>
Cc: Ian Lance Taylor <ia...@golang.org>, T L <tapi...@gmail.com>, golang-nuts <golan...@googlegroups.com>
Subject: Re: [go-nuts] Are short variable declarations necessary?
var could do that too:
(not that I'm advocating in favor of var in more places)
On Wed, Oct 19, 2016 at 11:17 PM, Michael Jones <michae...@gmail.com> wrote:
In fact, this example hints at something important. You could do as you say and create variables of *different* types which I find very general compared to the suggested var feature:
for a, b, c := 0, 0.5, big.NewFloat(0.5).SetPrec(200); a < 15; a++ {
(https://play.golang.org/p/OM7OC3R949)
-----Original Message-----
From: <golan...@googlegroups.com> on behalf of Ian Lance Taylor <ia...@golang.org>
Date: Wednesday, October 19, 2016 at 12:25 PM
To: Michael Jones <michae...@gmail.com>
Cc: T L <tapi...@gmail.com>, golang-nuts <golan...@googlegroups.com>
Subject: Re: [go-nuts] Are short variable declarations necessary?
You can create multiple local variables in the for scope by writing, for example
for a, b := 0, 10; a < b; a++ {
(That would work with var, too).
--
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...@googlegroups.com.
--
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.
-j
To answer the subject line question: No, short variable declarations are not technically necessary. We could have chosen not to have them. But there are good reasons for them. It was a deliberate design decision.Let me throw in a bit of historical perspective:Rob's invention of ":=" in one of his prior languages, and later, our shared experience with ":=" in Sawzall (https://en.wikipedia.org/wiki/Sawzall_(programming_language)) led to the desire of wanting ":=" in Go. Variable declaration and initialization is among the most common statements written in an imperative programming language (next to simple assignment), and ":=" worked beautifully and succinctly for this purpose in the past, and it fit well into the overall design of Go.The key here is that providing an initialization expression is mandatory with ":=" (in contrast to "just" a variable declaration), and because it's mandatory, we can omit the type of the variable (assuming we're ok with the type of the initialization expression).At the same time we wouldn't simple give up ordinary variable declarations, using the "var" keyword. For one, at the package level, a free-standing (i.e., w/o introductory keyword) "v := x" would cause parsing difficulties and be somewhat irregular compared to all other declarations.Thus, originally, "x := y" was simply very convenient syntactic sugar for "var x = y" and both could be used interchangeably, in most places. Go has little syntactic sugar, and where it has, it's because there's a lot of "bang for the buck" - and that's certainly the case for ":=".The fact that "var x = y" is not permitted as a "for" loop initialization statement was simply a design decision. Ian Taylor suggested that it's "ugly". Aesthetics plaid a role here; and different people find different things attractive. There's no hard technical reason why it couldn't be permitted, it just isn't.Finally, the special rule permitting "redeclarations" with ":=" only came in later. It gave short variable declarations different powers over regular keyword-based variable declarations. While it was considered a very useful new rule, it definitively distracted from the simple syntactic sugar regularity.It is no secret that many people (including us) think that there may be too many different yet similar ways of declaring and initializing variables in Go; and it would be nice if a future Go could streamline and simplify this. But we should also keep in mind that variable declaration and initialization, next to simple assignment, are the most common statements in program (it's one of the few things hardware can actually do directly, which is moving data from a to b), so it does not seem unreasonable to have various ways to express that, fine-tuned for each (relatively common) use case.- gri
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
On Friday, October 21, 2016 at 4:31:51 AM UTC+8, gri wrote:To answer the subject line question: No, short variable declarations are not technically necessary. We could have chosen not to have them. But there are good reasons for them. It was a deliberate design decision.Let me throw in a bit of historical perspective:Rob's invention of ":=" in one of his prior languages, and later, our shared experience with ":=" in Sawzall (https://en.wikipedia.org/wiki/Sawzall_(programming_language)) led to the desire of wanting ":=" in Go. Variable declaration and initialization is among the most common statements written in an imperative programming language (next to simple assignment), and ":=" worked beautifully and succinctly for this purpose in the past, and it fit well into the overall design of Go.The key here is that providing an initialization expression is mandatory with ":=" (in contrast to "just" a variable declaration), and because it's mandatory, we can omit the type of the variable (assuming we're ok with the type of the initialization expression).At the same time we wouldn't simple give up ordinary variable declarations, using the "var" keyword. For one, at the package level, a free-standing (i.e., w/o introductory keyword) "v := x" would cause parsing difficulties and be somewhat irregular compared to all other declarations.Thus, originally, "x := y" was simply very convenient syntactic sugar for "var x = y" and both could be used interchangeably, in most places. Go has little syntactic sugar, and where it has, it's because there's a lot of "bang for the buck" - and that's certainly the case for ":=".The fact that "var x = y" is not permitted as a "for" loop initialization statement was simply a design decision. Ian Taylor suggested that it's "ugly". Aesthetics plaid a role here; and different people find different things attractive. There's no hard technical reason why it couldn't be permitted, it just isn't.Finally, the special rule permitting "redeclarations" with ":=" only came in later. It gave short variable declarations different powers over regular keyword-based variable declarations. While it was considered a very useful new rule, it definitively distracted from the simple syntactic sugar regularity.It is no secret that many people (including us) think that there may be too many different yet similar ways of declaring and initializing variables in Go; and it would be nice if a future Go could streamline and simplify this. But we should also keep in mind that variable declaration and initialization, next to simple assignment, are the most common statements in program (it's one of the few things hardware can actually do directly, which is moving data from a to b), so it does not seem unreasonable to have various ways to express that, fine-tuned for each (relatively common) use case.- gri
Thanks, gri, this is almost the answer I want.
I still have two small questions about the short form.
1. would it be good to allow all identifiers in a short form are old ones (assume there is only one code block)?
2. would it be good/better to let identifiers in a short form not shadow outer block identifiers?
I know the second one will break compatibility. It is not a feature request. It is just a plain question.
There are already too many ways to declare and or assign a variable in Go. Adding more is not a solution.
-j
---j
There are already too many ways to declare and or assign a variable in Go. Adding more is not a solution.