func foo(r io.Reader) error {
data, err := io.ReadAll(r)
if err != nil {
return err
}
_, err = fmt.Print(data)
}
Extracting the fmt.Print line to a function it becomes:
func foo(r io.Reader) error {
data, err := io.ReadAll(r)
if err != nil {
return err
}
funcName(err, data)
}
func funcName(err error, data []byte) {
_, err = fmt.Print(data)
}
GoLand thinks it has to add err
as a parameter to funcName
because the err
variable is reused in foo
,
but we obviously don't want to pass the old err
in as a parameter.