← blog
#1Coding guidelinesA1

Naming

In Go, a name is good if it reads well.

Names should be short and self-explanatory. Avoid stuttering and unnecessary suffixes (and prefixes).
Otherwise, you will have painful-to-read code with strange names that are hard to reason about.

Everything except package names:

  • Length follows the scope. The further from its declaration a variable name is used, the more expressive it needs to be. It means, i is fine in short loop where you can see the declaration, but it is not fine two hundred lines later.
  • Do not stutter. Package name is part of the name. It means, transport.TransportConfig stutters, while transport.Config does not.
  • Drop suffixes that bring no useful meaning -> Impl, Base, Default, etc.
  • Use camelCase (unexported) and PascalCase (exported), even for constants. Not snake_case.

Package names:

  • Lowercase, one word, no underscores.
  • Package name should tell what it provides, not what data type or architecture layer it contains. discounts and inventory are acceptable, but controllers and services are (usually) not - more on this later.

Do your packages and types have good names? What are the main issues you encounter when naming packages?

Discuss on LinkedIn →