Skip to content

Instantly share code, notes, and snippets.

@tobstarr
Created October 6, 2014 09:29
Show Gist options
  • Save tobstarr/99230ae4132c0256ddbc to your computer and use it in GitHub Desktop.
Save tobstarr/99230ae4132c0256ddbc to your computer and use it in GitHub Desktop.

Naming error variables in go

In the go source code I read lately I found that most often err is used when it comes to naming error variables. I personally always preferred plain e (mainly because it is 66.6% shorter).

To increase my code compatibility I decided to get some data about that. Two greps (powered by silversearcher) on the code in the stdlib later I had some numbers I could work with:

	/usr/local/go/src$ e=$(ag " e := " | wc -l); echo $e
	334
	/usr/local/go/src$ err=$(ag " err := " | wc -l); echo $err
	4128
	/usr/local/go/src$ echo "100.0 * $err / ($e + $err)" | bc -l
	92.51456745853877185118

So I decided that 92.5% is definitely worth updating my snippets (and habits).

@davecheney was so kind to give me some detailed explanation about this unwritten naming convention: "tradition"

(Source: https://twitter.com/tobstarr/status/519037483439628289)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment