HN
Today

Go Naming Conventions: A Practical Guide

This guide lays out the essential naming conventions in Go, from identifiers to package and file names, emphasizing clarity and consistency. It delves into the nuances of exported vs. unexported identifiers, appropriate length, and avoiding "chatter" for more idiomatic Go code. For any Go developer, this resource is invaluable for writing code that is not only functional but also easily understood and maintained by others.

3
Score
0
Comments
#11
Highest Rank
11h
on Front Page
First Seen
Mar 28, 10:00 AM
Last Seen
Mar 28, 8:00 PM
Rank Over Time
1811111314172325272627

The Lowdown

The article by Alex Edwards serves as a comprehensive "Practical Guide" to Go naming conventions, underscoring the critical role good naming plays in code clarity, predictability, and navigability. It outlines both hard rules and widely accepted guidelines, demonstrating through examples what constitutes good and bad practices in Go programming.

  • Identifiers: Covers the rules for valid characters, avoiding Go keywords, and the conventional camelCase for unexported and PascalCase for exported identifiers. It emphasizes consistent acronym casing (e.g., APIKey), discourages non-ASCII characters, and advises against clashing with built-in types or standard library package names.
  • Exported and Unexported Identifiers: Explains how capitalization dictates exportability, impacting code visibility outside a package. It recommends defaulting to unexported identifiers to facilitate easier refactoring and promotes the concept of "shy code" from The Pragmatic Programmer.
  • Identifier Length and Descriptiveness: Advises tailoring identifier length to its scope, permitting short names for narrow contexts (like loop variables) and requiring more descriptive names for broader scopes, aiming for an optimal balance.
  • Naming Packages: Details conventions for package names, favoring lowercase ASCII, short, single-word nouns, and consolidated multi-word names (e.g., ordermanager). It strongly cautions against generic "catch-all" names like utils or helpers, which can lead to unclear scope and high