News for hackers. Stuff that matters.
Re:More Gotchas of Defer in Go, Part II by poorform on Thursday December 21, 2017 @01:04PM (#15981176)
This is too sneaky and seems like poor form.

I'm curious what good use-case there is for this, that the language bothered to support and allow this to even compile.

Duplicate variable name declaration + referencing in the same scope is unintuitive at best, and just seems wrong.

Re:More Gotchas of Defer in Go, Part II by pcwalton on Thursday December 21, 2017 @01:23PM (#15981392)
Shadowing can be useful if you want to make sure the "old" variable is never referenced again in the same scope.

The reason why this idiom feels "weird" is that the loop construct of the language really ought to automatically make a fresh instance of the loop counter for each trip through the loop.

Re:More Gotchas of Defer in Go, Part II by knorker on Thursday December 21, 2017 @02:19PM (#15981910)
I don't feel that it's in more poor form than the solution suggested in the article:

    for i := 0; i < 3; i++ {
      defer func(i int) {
       fmt.Println(i)
      }(i)
    }
This shadows `i` pretty much the same amount as what I wrote. If `i` gets a different name inside the lambda, then it'd also be worse because then you could accidentally use `i` still.
Re:More Gotchas of Defer in Go, Part II by tylersmith on Thursday December 21, 2017 @02:42PM (#15982120)
I personally prefer the version presented in the article, but your version has at least one nice feature in that you can cleanly specify at the top of the loop body which variables are being shadowed.