zlacker

[parent] [thread] 2 comments
1. thomas+(OP)[view] [source] 2025-08-22 12:43:22
Author here: I'm not talking about the value. I'm talking about the lifetime of the variable.

After checking for nil, there's no reason `err` should still be in scope. That's why it's recommended to write `if err := foo(); err != nil`, because after that, one cannot even accidentally refer to `err`.

I'm giving examples where Go syntactically does not allow you to limit the lifetime of the variable. The variable, not its value.

You are describing what happens. I have no problem with what happens, but with the language.

replies(1): >>zwnow+41
2. zwnow+41[view] [source] 2025-08-22 12:47:47
>>thomas+(OP)
Why does the lifetime even matter?
replies(1): >>thomas+p6
◧◩
3. thomas+p6[view] [source] [discussion] 2025-08-22 13:15:14
>>zwnow+41
I gave an example in the post, but to spell it out: Because a typo variable is not caught, e.g. as an unused variable.

The example from the blog post would fail, because `return err` referred to an `err` that was no longer in scope. It would syntactically prevent accidentally writing `foo99()` instead of `err := foo99()`.

[go to top]