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

What do you mean defer isn't scope based in Go?

(not super experienced Go developer)



In Go, defers are function scoped not block scoped.


I mean, you just write all your scopes as `(func() { })()` in go, and it works out fine.

Adding `func() {}()` scopes won't break existing code usually, though if you use 'break' or 'continue' you might have to make some changes to make it compile, like so:

https://go.dev/play/p/_Gq4QYtyMmp

see, no other issues, works exactly like you'd expect


Right, that makes more sense.





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

Search: