Skip to content

Instantly share code, notes, and snippets.

@freman
freman / errors.md
Last active September 18, 2018 12:53
Error handling as it can already be done

I'd like to argue that to a great degree, we can already do error handling the way the proposal demonstrates it in the 'corrected' new example through the tools we already have without introducing new keywords or new magic.

The following achieves the same error handling in almost exactly the same way without being much longer or uglier than the reference material.

func CopyFile(src, dst string) (err error) {
	defer func() {
		if err != nil {
			err = fmt.Errorf("copy %s %s: %v", src, dst, err)
 }