3 ms·
My least favorite feature is block scoping combined with the := operator. For example, this works: var x Foo x, err := doSomething() if err != nil {
by robfig 14y ago
My least favorite feature is block scoping combined with the := operator.
For example, this works:
var x Foo
x, err := doSomething()
if err != nil {
...
}
this does not:
var x Foo
if x, err := doSomething(); err != nil {
...
}
This pattern (or some variant) introduces more bugs in my code than I would like to admit.
- jgrahamc 14y agoWhat do you mean by 'does not work'? Your second example will work but the x declared in the if statement will be local to that statement (note not to the block but the entire statement). In both cases you have redeclared x. In the second the scope is different. See: http://play.golang.org/p/c9B-FXL0Fw http://play.golang.org/p/c9B-FXL0Fw