Identifiers, such as variable and package names, follow certain rules.
The convention in Go is to use MixedCaps
or mixedCaps
rather than underscores to write multiword names.
Words in names that are initialisms or acronyms (e.g. "URL" or "NATO") have a consistent case.
For example : - "URL" should appear as "URL" or "url" (as in "urlPony", or "URLPony"), never as "Url". - ServeHTTP should be used and not ServeHttp. - For identifiers with multiple initialized "words", For example use "xmlHTTPRequest" or "XMLHTTPRequest".
Variable names in Go should be short rather than long. This is especially true for local variables with limited scope. Prefer c
to lineCount
. Prefer i
to sliceIndex
.
The basic rule:
- The further from its declaration that a name is used, the more descriptive the name must be.
- For a method receiver, one or two letters is sufficient. Common variables such as loop indices and readers can be a single letter (i
, r
).
- More unusual things and global variables need more descriptive names.