3 ms·
Can someone ELI12 what's happening here? I haven't had the pleasure of learning Go yet.
by Sohcahtoa82 4y ago
Can someone ELI12 what's happening here? I haven't had the pleasure of learning Go yet.
- room271 4y agoGo is changing the semantics of its for loops and the user is not a fan of this - at least in the subtleties. Most people, from what I can tell, are very much for this change. And note, Go is introducing quite sophisticated backwards compatibility features to avoid changing semantics for programs that are need the old behaviour.
- tapirl 3y agoNot exactly. I'm fan of the change for "for-range" loops, but not a fan for the change for "for;;" loops.
- HillRat 4y agoGo loops define pointers for scope-defined variables at the loop, rather than iteration, level, so there's a common "gotcha" where you intern a pointer to access it outside of the loop (such as pushing it onto an array), and then discover that its value is clobbered by subsequent runs of the loop. There is a proposal to change this, since it's a source of confusion and bugs, particularly when `range`ing over pointers. What the code snippet demonstrates is that, for a contrived scenario, the proposal in question also creates an unexpected result when you expect pointers to remain stable between loop iterations. To be honest, I can't think of any time where this would be an issue for me (as opposed to writing `foo := foo`); Go has quite a bit of cognitive friction in its loop semantics, so anything to reduce unexpected behavior is welcome to my mind.
- tapirl 3y agoIn short, the new proposed Go semantics automatically declared an inside variable which has the same name as the iteration variable "I". So the inside "I" is not the iteration variable "I".