3 ms·
>The loop iteration variable is reused across iterations, so capturing it by reference (the default for closures) is likely to lead to bugs. >defer inside a bl
by omginternets 5y ago
>The loop iteration variable is reused across iterations, so capturing it by reference (the default for closures) is likely to lead to bugs.
>defer inside a block executes not at the end of the block, but at the end of the enclosing function.
>defer evaluates sub-expressions eagerly.
Whenever I see this kind of complaint, I can't shake the feeling that the author struggles to distinguish between "bad design" and "learning how things work". There are many fair criticisms to levy against Go, but "it didn't behave as I first expected" is hardly one of them. If it were, there would be lots more to complain about in the languages that are routinely touted as superior to Go, namely: Rust and Haskell.
- boolemancer 5y agoI disagree. Counterintuitive design is bad design, and leads to subtle bugs getting introduced. This is especially damning in a language where so many decisions are justified by a desire for simplicity and ease of understanding. You throw that all out the window when you make design decisions that are opposite to most people's expectation.
- Andys 5y agoI get what you're saying, but taken to an extreme, every language would be the same (or, we'd only have one language). After you get used to a subjective alternative it becomes the new normal.