logoalt Hacker News

Go Naming Conventions: A Practical Guide

60 pointsby yurivishlast Tuesday at 8:49 PM32 commentsview on HN

Comments

laserlighttoday at 1:40 PM

> we use the identifier p to represent a value in the people slice — the range block is so small and tight that using a single letter name is clear enough.

No, it's not. When you see `p.Age`, you have to go back and find the body of the loop, see what it operates on and decipher what p stands for. When you see `person.Age`, you understand it. I've never understood what is gained by using `p` instead of spelling it out as `person`.

show 6 replies
qezztoday at 12:20 PM

I was surprised to see literally invalid names in the "bad" section, e.g. "Cannot start with a digit". Why even presenting this if it's rejected by the compiler?

show 3 replies
red_admiraltoday at 11:03 AM

Another of mine: don't name a struct after an interface method that it's supposed to implement. If you have a package linearalgebra, then making a custom error type linearalgebra.LinearAlgebraError is too "chatty" but linearalgebra.Error will cause you pain if it implements "Error string()", as it probably should, and you decide to make a linearalgebra.MatrixSingularError that wraps a linearalgebra.Error to "inherit" its methods.

In the end, it ended up called linearalgebra.Err .

P.S Alex Edwards' "let's go" and "let's go further" are great books to get someone up to date with golang, just keep an eye on features that are newer than the book(s).

show 1 reply
nasretdinovtoday at 12:08 PM

I like this article — short, accurate (which is somehow not a given these days...) and useful, just like Go language itself.

piekvorsttoday at 2:44 PM

[dead]

jeninhotoday at 4:00 PM

[dead]

nacozarinatoday at 11:34 AM

[flagged]

show 5 replies