🚀 Want to write clear, maintainable Go code? Follow these naming conventions to make your code not just work, but shine! 💎
- Uppercase Initial (ExportedName): Makes functions or variables exported and accessible outside the package, like
public
in other languages. - Lowercase Initial (unexportedName): Keeps them unexported, accessible only within the package.
Example:
// Exported: visible outside the package
func PublicFunction() {}
// Unexported: only visible within the package
func privateFunction() {}
- Short, descriptive names: Go prefers concise, meaningful names, like n, err, or buf.
- Avoid abbreviations: Full words increase clarity (userCount > usrCnt).
- CamelCase for functions and variables: Keep it camelCase, no underscores.
Example:
var userCount int // Good
func calculateTotal() {}
- Package names are short, lowercase, and singular (e.g., fmt, strings).
- They shouldn’t need prefixes when used, as in fmt.Println.
- End in -er for action-based interfaces (Reader, Writer).
- Use broader names for more complex interfaces.
Example:
type Shape interface {
Area() float64
Perimeter() float64
}
- Use
SNAKE_CASE
for global constants, fully uppercase. - Local constants can be lowercase.
Example:
const PI = 3.14159
const maxUsers = 100
- Use
err
for error variables and anErr
prefix for error constants to make them easily identifiable.
Example:
var ErrNotFound = errors.New("not found")
- Capitalization: Use uppercase initials for exported names and lowercase initials for unexported names.
- Short Names: Keep names descriptive and concise.
- Packages: Use short, lowercase names.
- Interfaces: Use
-er
suffix for action-based interfaces (e.g.,Reader
,Writer
). - Constants: Use
SNAKE_CASE
for global constants and lowercase for local constants. - Errors: Use
err
for error variables andErr
prefix for error constants.
Try it out! 🚀 These conventions make Go code cleaner, simpler, and easier to work with. Let’s make Go code beautiful, one convention at a time! ✨