Comment by akoboldfrying
1 day ago
Can't you create a new block scope in Go? If not, I agree. If so, just do that if you want lexical scoping?
1 day ago
Can't you create a new block scope in Go? If not, I agree. If so, just do that if you want lexical scoping?
defer is not block scoped in Go, it's function scoped. So if you want to defer a mutex unlock it will only be executed at the end of the function even if placed in a block. This means you can't do this (sketch):
You can call Unlock directly, but then if there's a panic it won't be unlocked like it would be in the above. That can be an issue if something higher in the call stack prevents the panic from crashing the entire program, it would leave your system in a bad state.
This is the key problem with defer. It operates a lot like a finally block, but only on function exit which means it's not actually suited to the task.
And as the sibling pointed out, you could use an anonymous function that's immediately called, but that's just awkward, even if it has become idiomatic.
You have to create an anonymous function.