Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Do you mind explaining why defer is a problem in Go? I only have experience with Java’s try-with-resources which I like a lot, it looks like C# using is similar.


I’m not the commenter but I too am not a fan.

1. You cannot remotely easily wire them in a SIGINT scenario. I have done it, it was a load of plumbing and I wrote an equivalent to Java’s shutdown hook and tied it to sigterm and sigint then called that same hook in the normal shutdown path to simulate the defer call cycle.

2. “using” or “with” in other languages is nice and explicit about when it runs and you can easily run it before your function exits. In the case of go you need to wrap some function to force this same behavior in Go and it looks very forced.

Defer is fine, especially if you’re writing something with a simple life cycle. I just end up using it less when I need to support cleanup functions being called no matter what in a graceful shutdown.


ad.2 defer in go is function-scoped, but in zig block-scoped, so you can create block in zig to defer earlier?


Interesting, I can see those 2 issues.


go's defer is braindead because of its scoping.

Zig's defer is block scoped, so you can actually use it much more easily.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: