2 ms·
> a defer in an if-block executes instantly, so you cannot conditionally defer anymore. Of course you can, using ?: (or && and || if you prefer), just like any
by lmm 2y ago
> a defer in an if-block executes instantly, so you cannot conditionally defer anymore.
Of course you can, using ?: (or && and || if you prefer), just like any other case where you want an expression rather than a statement. Or simply using the non-block form of if. (Some stupid autoformatters or tech leads insert extraneous braces, but you should be avoiding those already).
- rollulus 2y agoThe parent I was replying to was talking about Go. There is no ternary operator in Go. There are no non-block forms of if in Go. I'm not sure what your "of course" is referring to, but I guess it is unrelated.
- lmm 2y agoI figured we were talking about C since that's the main topic. In any case short-circuit && and || still work in Go AIUI, so my point stands.