3 ms·
As much as I want them to fix the big things like lack of generics, I hope they fix some of the little things that the compiler doesn't catch but could/should.
by lemoncucumber 9y ago
As much as I want them to fix the big things like lack of generics, I hope they fix some of the little things that the compiler doesn't catch but could/should. One that comes to mind is how easy it is to accidentally write:
for foo := range(bar)
Instead of:
for _, foo := range(bar)
When you just want to iterate over the contents of a slice and don't care about the indices. Failing to unpack both the index and the value should be a compile error.
- mappu 9y agoIf `bar` is a slice then the short syntax isn't that useful (although it's shorter than the three-clause for loop equivalent). But if `bar` is a map or a channel, then that short syntax is very handy. For comparison's sake, you don't see many PHP users complaining about the difference between `foreach($bar as $foo)` and `foreach($bar as $_ => $foo)`.
- lemoncucumber 9y agoI'd also be fine with it if they switched the ordering of the tuple so that only unpacking one thing gave you the element instead of the index. My point is mainly that the behavior you get currently with a slice is not what you want 9 times out of 10.
- lemoncucumber 9y agoHere's a toy version of a real bug that I wasted a bit of time debugging which was due to this behavior: https://play.golang.org/p/6pBUPBTTvj https://play.golang.org/p/6pBUPBTTvj
- akavel 9y agoWould be cool if you could consider doing a writeup about it, and linking it on the mentioned wiki page.
- mmargerum 9y agoI've got caught by this more than once. Again, implicit behavior sucks so I suggest they get rid of the first form.