Live data from Hacker News

More Gotchas of Defer in Go, Part II

blog.learngoprogramming.com

51–55 of 55 posts

Re: More Gotchas of Defer in Go, Part II

#51
post #50
post #36

Earlier quoted context omitted.

Crikey! You wouldn't wanna allocate closures in a loop anyway, would you? Even if the compiler is smart about it. Doesn't read so well either.. what's wrong with adding a simple argument to your anonymous-func allocated outside of a loop? It's readable, and there are no 'gotchas'. The args are evaluated at the point of defer/go, not func execution. Simples. Shadowing bites you sooner or later if it becomes a habit an…

> Crikey! You wouldn't wanna allocate closures in a loop anyway, would you? Very often yes I do. Go encourages synchronous APIs, making it up to the caller to add concurrency. This is great, in my opinion. E.g. this is a common pattern: var wg sync.WaitGroup ch := make(chan int, len(items)) for _, item := range items { item := item wg.Add(1) go func() { defer wg.Done() ch Similar patterns with defer, although yes I'd…

None of your explanations/examples counter the fact that you could also declare your anonymous-func just above your loop in a local, ie. `doit:= func(item...` and then simply `go doit(item)` in the loop. You get: a leaner terser loop to later have to read through, no iteration-scope "gotcha", no need to elaborately spell out explicit shadowings for what are naturally semantically really func "args" already/anyway, at worst identical cost (or better)..

But well, guess it comes down to subjective stylistic preferences here =)

> So that even if "do something" throws exception… err… panics… the file gets closed.

Your defer as placed in your above example is already scheduled to run always, even on a later panic. (After all, how else could one `recover` from a `panic` if it wasn't for `defer`?) I don't see the point of the double-closing at all here..

Re: More Gotchas of Defer in Go, Part II

#52

I code in go every day professionally. These are not gotchas, they are well defined behaviors.

Check out the first part too. Btw, gotcha means what you say: "a gotcha ... works as documented but is counter-intuitive and almost invites mistakes ..." https://en.wikipedia.org/wiki/Gotcha_(programming)

Re: More Gotchas of Defer in Go, Part II

#53
post #51
post #50

Earlier quoted context omitted.

> Crikey! You wouldn't wanna allocate closures in a loop anyway, would you? Very often yes I do. Go encourages synchronous APIs, making it up to the caller to add concurrency. This is great, in my opinion. E.g. this is a common pattern: var wg sync.WaitGroup ch := make(chan int, len(items)) for _, item := range items { item := item wg.Add(1) go func() { defer wg.Done() ch Similar patterns with defer, although yes I'd…

None of your explanations/examples counter the fact that you could also declare your anonymous-func just above your loop in a local, ie. `doit:= func(item...` and then simply `go doit(item)` in the loop. You get: a leaner terser loop to later have to read through, no iteration-scope "gotcha", no need to elaborately spell out explicit shadowings for what are naturally semantically really func "args" already / anyway ,…

Yeah, I could declare it above. But it's subjectively harder to read, having to jump around. Like you say: subjective.

> Your defer as placed in your above example is already scheduled to run always

Yes, brainfart. Sorry. This is from a completely separate recommendation to defer a close (dropping error return), but also manually close so that closing errors can be surfaced. Matters for e.g. writing files (not so much reading), especially when you don't flush manually.

Re: More Gotchas of Defer in Go, Part II

#54
post #49

Earlier quoted context omitted.

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

PHP also has a garbage collector. Swift and D also have garbage collectors and support RAII. And since I already see it coming, reference counting is a garbage collection implementation algorithm in computer science literature.

Yep, and I even referred to reference counting earlier in the week as garbage collection, so I'm going to assume I was having memory corruption with my answer. :-)

Re: More Gotchas of Defer in Go, Part II

#55
post #49

Earlier quoted context omitted.

PHP also has a garbage collector. Swift and D also have garbage collectors and support RAII. And since I already see it coming, reference counting is a garbage collection implementation algorithm in computer science literature.

Yep, and I even referred to reference counting earlier in the week as garbage collection, so I'm going to assume I was having memory corruption with my answer. :-)

Hehe :)
Post reply on HN