Create a reader which returns an error on Close

type reader struct{} func (r reader) Close() error {

return errors.New("Close Error")

}

Let’s try

reader always returns an error when its Close() method is called and release() will call it inside defer as seen in the image above.

r := reader{} err := release(r) fmt.Print(err)

Output

nil

err is nil but we were expecting: “Close Error”.

Why is that?

Assignment inside the deferred func’s if block shadows the named result value err with a new err variable. So, release() returns the original err result-value.

Solution