The reason for short variable declarations is, that it makes reuse easier in cases of multiple return values. e.g.
var a, err = Foo() if err != nil { return err } var b, err = a.Bar() if err != nil { return err } doesn't work. That being said, the duplication between ways of declaring variables has long been acknowledged as an unfortunate historical accident. On Tue, Oct 18, 2016 at 5:09 PM, T L <tapir....@gmail.com> wrote: > > > On Tuesday, October 18, 2016 at 11:01:18 PM UTC+8, Pietro Gagliardi > (andlabs) wrote: >> >> No, the reason for short variable declarations is to avoid having to >> stutter the type of variables everywhere. >> > > You can also avoid having to stutter the type of variables by using var > declaration. > > >> It's part of the reason why Go is strongly typed yet doesn't fully feel >> that way, and was one of the main design goals at first. >> > >> Why the control statements require one, however, is something I wouldn't >> know. >> > On Oct 18, 2016, at 10:50 AM, T L <tapi...@gmail.com> wrote: >> >> >> >> On Tuesday, October 18, 2016 at 10:40:02 PM UTC+8, Ian Lance Taylor wrote: >>> >>> On Tue, Oct 18, 2016 at 7:21 AM, T L <tapi...@gmail.com> wrote: >>> > >>> > alternative question, why followings are not accepted in syntax: >>> > >>> > if var x = 5; x > 3 { >>> > _ = x >>> > } >>> > >>> > for var x = range []int{0,1,2} { >>> > _ = x >>> > >>> > } >>> > >>> > switch var x = "abc"; x { >>> > default: >>> > _ = x >>> > } >>> > >>> > switch var x = (interface{}(true)).(type) { >>> > default: >>> > _ = x >>> > } >>> >>> That syntax adds no functionality and, at least to me, seems uglier >>> and harder to read. >>> >>> Ian >>> >> >> So the reason of adding short variable declarations is just to avoid >> so-called ugliness? >> >> >> -- >> 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. >> >> >> -- > 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+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/d/optout. > -- 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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.