Canonical guidance

Use when

Avoid

Preferred pattern

func run(name string) (err error) {
	f, err := os.Open(name)
	if err != nil {
		return err
	}
	defer func() {
		if cerr := f.Close(); err == nil {
			err = cerr
		}
	}()

	return useFile(f)
}

Anti-pattern

Explanation: This anti-pattern is tempting because named results can remove repetition, but hidden mutation usually costs more readability than it saves.

Why

Related pages

Sources