🧩 Part 2 of the Idiomatic Go Series — see Part 1: The Power of Idiomatic Go
Naming in Go isn’t just about style — it’s about writing idiomatic code that’s clear, predictable, and Go-like. This cheat sheet lays out key naming conventions for variables, types, interfaces, and packages, so you can avoid common pitfalls and write cleaner code with confidence. Whether you’re new to Go or refining your best practices, this guide has you covered.
📌 Context and Cancellation
Go’s concurrency model relies on context, and clear naming keeps things tight.
Purpose | Idiomatic Name |
---|---|
Context variable | ctx |
Cancel function | cancel |
Example:
// Use 'ctx' and 'cancel' for clarity
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
📌 Error Handling
Errors are central to Go—keep their names simple to focus on the logic.
Purpose | Idiomatic Name |
---|---|
Error variable | err |
Example:
// 'err' as the last return value
result, err := doSomething()
if err != nil {
return err
}
✅ Tip: Errors are returned as the last return value in Go functions.
📌 Loop Variables
Short names for loops make iteration clean and intuitive.
Purpose | Idiomatic Name |
---|---|
Index in loop |
i , j
|
Value in loop |
v , val
|
Key in a map |
k , key
|
Example:
// 'i' and 'v' for index and value
for i, v := range items {
fmt.Println(i, v)
}
📌 Receivers
Receiver names should be concise and tied to the type.
Type Name | Receiver Name |
---|---|
User |
u |
Server |
s |
Client |
c |
Example:
// Short 'u' for User type
func (u *User) FullName() string {
return u.First + " " + u.Last
}
🧠Note: Use 1-letter lowercase receivers for brevity and clarity.
📌 Constants
Constants in Go follow camelCase for unexported names and UpperCamelCase for exported ones.
Purpose | Idiomatic Name |
---|---|
Exported constant | MaxRetries |
Unexported constant | maxBufferSize |
Example:
const MaxRetries = 3
const defaultTimeout = 30 * time.Second
✅ Tip: Use descriptive names for constants to reflect their purpose, and avoid single-letter names like c
or k
.
📌 Package Naming
Package names should be short, lowercase, and descriptive, avoiding stuttering (e.g., don’t name a package userutils
if it’s for User
types).
Purpose | Idiomatic Name |
---|---|
Package name | log, http, user |
Example:
import "github.com/example/user"
✅ Tip: Keep package names singular (e.g., user
not users
) and avoid generic terms like utils
.
📌 Common Short Names
These shorthand names are standard across Go projects.
Meaning | Name |
---|---|
Temporary value | tmp |
Buffer | buf |
Configuration | cfg |
Request | req |
Response | resp |
Logger |
log , logger
|
📌 Boolean Naming
Booleans should read naturally in if statements.
Purpose | Idiomatic Name |
---|---|
Flag for readiness | isReady |
Permission check | hasPermission |
Retry condition | shouldRetry |
Example:
// Clear names for boolean logic
if isReady && hasPermission {
doSomething()
}
✅ Tip: Name booleans to imply their truthy state.
🧵 Wrapping Up
Naming conventions are a subtle but powerful part of idiomatic Go. These short, predictable patterns make your code easier to read, review, and maintain — especially when working across teams.
This cheat sheet gives you the patterns to make your code feel like it belongs in the Go ecosystem.
💬 Got a favorite Go naming convention or a tricky naming challenge? Share it below—let’s make this cheat sheet even better together!
Â
💡 Curious why idiomatic Go matters?
Start with Part 1: The Power of Idiomatic Go — it’s the foundation for this cheat sheet.
📚 Want to go even deeper? Explore Effective Go and the Go Style Guide — they’re foundational reads for writing Go the idiomatic way.
Â
Â
Â
Top comments (4)
Super handy guide. Naming in Go feels deceptively simple, but getting it right takes practice. Bookmarked!
Appreciate that! Naming’s one of those deceptively tricky things in Go — glad the cheat sheet hit the mark. :)
Very useful, thanks for taking the time to write that.
Appreciate it—thanks for reading!