Goland shows a underline in a err variable without explaining why/no tooltip for the underline cause (see image below -- the arrow points to the weird underline).
Does anyone know the cause for the underline?
(It does not seem to be because the err is defined a few lines above because I have similar err reuse in other files and there is no underline in them).
Here is the code, although this question would make no sense without an explaining picture from the IDE because this seems to be a bug in Goland.
package mypack
import (
"fmt"
"os"
)
func SomeFunc() (string, error) {
err := GetSomething()
if err != nil {
return "", fmt.Errorf("some err")
}
currentDirectory, err := os.Getwd()
if err != nil {
return "", fmt.Errorf("error getting current dir. %v", err)
}
return currentDirectory, nil
}
func GetSomething() error {
return nil
}
The underlined variable is not an indicator of an error. GoLand is informing you that you are re-assigning the err
variable.
You first create the variable here:
err := GetSomething()
And then you re-assign the same variable in that line:
currentDirectory, err := os.Getwd()
And that is why the err
is underlined. Not an error, just something to make this more obvious to you.
Sometimes, it is undesirable to re-assign variables, because it can have unwanted side-effects.
In this particular case, I think it is a common Go pattern to have a single err
variable and re-use it throughout a function/method.