Identifiers
The Go standard library names some function with the old (Unix) names whileothers are in CamelCase. The convention is to leave well-known legacynot-quite-words alone rather than try to figure out where the capital lettersgo: Atoi
, Getwd
, Chmod
. CamelCasing works best when you have whole wordsto work with: ReadFile
, NewWriter
, MakeSlice
. The convention in Go is touse CamelCase rather than underscores to write multi-word names.
As we did above in our myeven
program, accessing content from an imported(with import
) package is done with using the package’sname and then a dot. After import "bytes"
the importingprogram can talk about bytes.Buffer
. A package name should be good, short,concise and evocative. The convention in Go is that package names are lowercase,single word names.
The package name used in the import
statement is the default name used. But ifthe need arises (two different packages with the same name for instance), youcan override this default: import bar "bytes"
The function Buffer
is nowaccessed as bar.Buffer
.
Another convention is that the package name is the base name of its sourcedirectory; the package in src/compress/gzip
is imported as compress/gzip
buthas name gzip
, not compress/gzip
.
It is important to avoid stuttering when naming things. For instance, thebuffered reader type in the bufio
package is calledReader
, not BufReader
, because users see it as bufio.Reader
, which isa clear, concise name.
Similarly, the function to make new instances of ring.Ring
(packagecontainer/ring
), would normally be called NewRing
, but since Ring
is theonly type exported by the package, and since the package is calledring
, it’s called just New
. Clients of the package seethat as ring.New
. Use the package structure to help you choose good names.
Another short example is once.Do
(see package sync
); once.Do(setup)
readswell and would not be improved by writing once.DoOrWaitUntilDone(setup)
. Longnames don’t automatically make things more readable.