Live data from Hacker News

More Gotchas of Defer in Go, Part II

blog.learngoprogramming.com

1–10 of 55 posts

Re: More Gotchas of Defer in Go, Part II

#2
Every point mentioned was not a gotcha to anyone who read the official introductory tour of the language. The behaviour may be unexpected to newbies, but if you spent an hour or so learning the language, you shouldn't be falling for these.

Re: More Gotchas of Defer in Go, Part II

#3
Also read the official blog post on defer: https://blog.golang.org/defer-panic-and-recover

It covers about half of these gotchas by laying out how the defer statement works in plain English. The other half (like how closures work inside loops in Go) are covered elsewhere in the language tour.

I do really like the visualizations! Makes it very clear how these mechanics work.

Re: More Gotchas of Defer in Go, Part II

#4
I think most of the people who understand the basic principles of how computers (and many languages) work wouldn’t be surprised by any of the „gotcha” described in this article. It seems to be written for people who just started working with golang without any or little knowledge about information technology.

Re: More Gotchas of Defer in Go, Part II

#6
post #4

I think most of the people who understand the basic principles of how computers (and many languages) work wouldn’t be surprised by any of the „gotcha” described in this article. It seems to be written for people who just started working with golang without any or little knowledge about information technology.

The semantics of many languages' for loops are explicitly designed to avoid this problem: for example, for-of in ES6. Generally, modern language design considers the interaction between loop iteration variables and closure captures. It's reasonable to criticize Go as a language for not addressing this problem rather than users.

Re: More Gotchas of Defer in Go, Part II

#7
post #4

I think most of the people who understand the basic principles of how computers (and many languages) work wouldn’t be surprised by any of the „gotcha” described in this article. It seems to be written for people who just started working with golang without any or little knowledge about information technology.

I am not sure about this. Swift for example also has `defer` statements but its behavior differs from Go's. Swift executes defer statements at the end of the block, not the end of the function. And AFAICR Swift doesn't evaluate the parameters right away like Go does. If you have already read how defer works in detail in Go, you probably already know this. Devs new to the language or those that haven't used defer in those cases might still be surprised when this shows up.

Re: More Gotchas of Defer in Go, Part II

#9
post #3

Also read the official blog post on defer: https://blog.golang.org/defer-panic-and-recover It covers about half of these gotchas by laying out how the defer statement works in plain English. The other half (like how closures work inside loops in Go) are covered elsewhere in the language tour. I do really like the visualizations! Makes it very clear how these mechanics work.

The content is good, and it is accurate, but even though defer can be a bit tricky this blog post series seems to literally be describing the entire semantics of "defer" as "gotchas". It's not that tricky!

Re: More Gotchas of Defer in Go, Part II

#10
post #5

#4 can also be fixed with: for i := 0; i

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.

Post reply on HN