Live data from Hacker News

More Gotchas of Defer in Go, Part II

blog.learngoprogramming.com

21–30 of 55 posts

Re: More Gotchas of Defer in Go, Part II

#21
post #17

Earlier quoted context omitted.

Collecting tasks from all iterations to await before returning.

Fair enough. But it's something like 5 lines of code to write that manually, and writing it explicitly is clearer. With implicit function-scoped defer, someone might refactor the code to inline the body of the function into its caller and break it.

I totally agree with you.

Re: More Gotchas of Defer in Go, Part II

#22
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 t…

[deleted]

Re: More Gotchas of Defer in Go, Part II

#23
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 .

I don't feel that it's in more poor form than the solution suggested in the article:

    for i := 0; 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

#24
post #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!

Well, it depends. Upgoing posts will go deeper on defer.

Re: More Gotchas of Defer in Go, Part II

#25
post #23

Earlier quoted context omitted.

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 .

I don't feel that it's in more poor form than the solution suggested in the article: for i := 0; 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.

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.

Re: More Gotchas of Defer in Go, Part II

#26
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.

Thanks! That post definitely a must read.

Btw, the first part was more advanced (but again for beginners and intermediate gophers) -> https://blog.learngoprogramming.com/gotchas-of-defer-in-go-1...

Re: More Gotchas of Defer in Go, Part II

#27
post #8

It's indefensible that defer works on the function and not on the scope.

Both seem like fair options to me. With function scope, you can

    func f() {
      x := ...
      if x.something() {
        x.doSomethingEarlier()
        defer x.cleanup()
      }
      // use x however you like
    }
where scope-based forces you to do stuff like

    func f() {
      x := ...
      if x.something() {
        x.doSomethingEarlier()
        defer x.cleanup()
        // use x however you like
      } else {
        // use x however you like
      }
    }
In a scope-based defer, you'd have to keep all related code in the scope, nesting it another layer deeper / possibly duplicating it.

On the flip-side is of course that this doesn't work like most would probably want in function-scoped:

    for i := 0; i 
and you're forced to

    for i := 0; i 
I've seen both of these patterns pretty frequently, in Go and in other languages. Go could, of course, have both a func_defer and a scope_defer, but that doesn't seem like it'd fit with the fairly strong focus on keeping the language feeling small and simple. So they had to pick one, and it can't handle both cases.

Re: More Gotchas of Defer in Go, Part II

#29
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.

In only surprised because so many of them are such well-known things to avoid when designing a language and runtime.

Re: More Gotchas of Defer in Go, Part II

#30

Defers just feel like a watered down version of what you get with good scoping and RAII. They're a half measure for something programming languages solved decades ago.

Go has a garbage collector. RAII is only viable in languages where destruction is predictable, like C++ or PHP.
Post reply on HN