Go Naming Conventions
Go uses naming to encode visibility (UpperCamelCase = exported, lowerCamelCase = unexported), so naming is load-bearing — not cosmetic. Names should be short, contextual, and non-repetitive. The package name is always present at the call site; pretending otherwise is the single biggest source of bad Go names.
Core Rules
- MixedCaps only. No underscores, no
SCREAMING_SNAKE_CASE, no kHungarian. Exceptions: test subtests (TestFoo_BadInput), generated code, cgo.
- Capitalization is visibility.
Exported, unexported. Do not invent other conventions.
- No stuttering. The package name is at the call site;
http.HTTPClient is wrong, http.Client is right.
- Scope drives length.
i is fine in a 3-line loop; package-level vars need descriptive names.
- Initialisms keep one case.
userID, HTTPServer, ParseURL — never userId, HttpServer, ParseUrl.
- Receivers are 1-2 letter abbreviations, consistent across all methods of the type. Never
this/self.
Naming Decision Flow
What are you naming?
├─ Package → lowercase single word, singular, specific (not util/common/helper)
├─ File → lowercase, underscores OK (user_handler.go)
├─ Interface → method + "-er" when single-method (Reader, Closer, Stringer)
├─ Struct/Type → MixedCaps noun (Request, FileHeader)
├─ Constructor → New() if package has one primary type; NewThing() if multiple
├─ Constant → MixedCaps; never ALL_CAPS; role-based not value-based
├─ Enum (iota) → type-prefix + Unknown/Invalid at position 0
├─ Sentinel error → ErrXxx (var ErrNotFound = errors.New("..."))
├─ Error type → XxxError (type PathError struct{})
├─ Boolean field → is/has/can prefix (isReady, hasPerm)
├─ Getter → field name only (Owner()), never GetOwner()
├─ Setter → SetXxx (SetOwner)
├─ Option → WithXxx (WithLogger, WithPort)
├─ Variant → WithContext suffix, In suffix (in-place), Must prefix (panics)
└─ Variable → length proportional to scope distance
Quick Reference Table
| Element |
Convention |
Example |
| Package |
lowercase, singular |
http, tabwriter |
| Exported |
UpperCamelCase |
ReadAll, HTTPClient |
| Unexported |
lowerCamelCase |
parseToken, userCount |
| Receiver |
1-2 letters |
func (s *Server) |
| Constant |
MixedCaps |
MaxRetries, defaultTimeout |
| Initialism |
uniform case |
URL, HTTPServer, xmlParser |
| Sentinel error |
Err prefix |
ErrNotFound |
| Error type |
Error suffix |
*PathError |
| Boolean field |
is/has/can |
isConnected |
| Option func |
With + field |
WithPort(8080) |
| Format func |
f suffix |
Errorf, Wrapf |
Frequently Missed Conventions
These are correct but non-obvious — they account for most naming mistakes in code review.
Constructor: New vs NewThing
If the package exports one primary type, the constructor is New(). Callers write apiclient.New(), not apiclient.NewClient(). Only use NewThing when the package builds several things (http.NewRequest, http.NewServeMux).
Boolean Fields Get a Prefix
Unexported boolean fields use is/has/can. A bare adjective is ambiguous — is connected a method or a field, a state or a verb past tense?
type Conn struct { isOpen bool }
func (c *Conn) IsOpen() bool { return c.isOpen }
Error Strings Are Fully Lowercase
Including acronyms. Errors get concatenated: fmt.Errorf("parsing token: %w", err) becomes "parsing token: invalid message id". Mid-sentence capitals look wrong. Use "invalid message id" not "invalid message ID".
Sentinel errors should include the package name: errors.New("apiclient: not found").
Enum Zero Value Is a Sentinel
var s Status is silently 0. If 0 is StatusReady, uninitialised values look intentional. Put StatusUnknown (or Invalid) at iota 0.
type Status int
const (
StatusUnknown Status = iota // zero-value catch
StatusReady
StatusRunning
)
Subtest Names Are Lowercase Phrases
t.Run("valid id", ...) // not "Valid ID"
t.Run("empty input", ...)
Read references/types-errors-constants.md when naming new struct/interface/enum/error families.
MixedCaps Is Load-Bearing
MaxPacketSize // good
userCount // good
parseHTTPResponse // good
MAX_PACKET_SIZE // wrong — Go reserves casing for visibility
max_packet_size // wrong — snake_case
kMaxBufferSize // wrong — Hungarian
Avoid Stuttering
The package name is always present at the call site.
// In package http
type Client struct{} // not HTTPClient — caller writes http.Client
// In package user
func New() *User // not NewUser — caller writes user.New()
// In package dbpool
type Pool struct{} // not DBPool
type Option func() // not PoolOption
Read references/identifiers-and-scope.md for receivers, variable scope rules, and import aliasing.
Avoid Built-In Names
Never shadow error, string, len, cap, append, copy, new, make, nil, iota. The compiler allows it; readers and tools do not.
Anti-Patterns
| Mistake |
Fix |
MAX_RETRIES = 3 constant |
MaxRetries = 3 — MixedCaps |
GetName() string getter |
Name() string — Go omits Get |
HttpClient, UserId, ParseUrl |
HTTPClient, UserID, ParseURL — uniform initialism case |
this/self receiver |
One-letter abbreviation (s for Server) |
util, common, helpers package |
Specific name that describes content (stringutil, httpauth) |
user.NewUser() constructor |
user.New() — drop the type name |
connected bool field |
isConnected bool — prefix reads as a question |
"invalid message ID" error |
"invalid message id" — fully lowercase |
StatusReady at iota 0 |
Add StatusUnknown at 0 |
userSlice []User |
users []User — types do not belong in names |
Verification Checklist
Enforce With Linters
Most rules are mechanical and a linter will catch them in CI:
revive — var-naming, exported, receiver-naming, error-naming.
predeclared — flags identifiers that shadow built-ins.
errname — enforces ErrXxx / *XxxError.
misspell — keeps comments and identifiers consistent.
Add them to .golangci.yml and run golangci-lint run in CI.
References
- references/identifiers-and-scope.md — receivers, scope-based length, acronyms, import aliasing
- references/types-errors-constants.md — interfaces, structs, enums, sentinel vs typed errors
- references/functions-and-options.md — constructors, getters, variants, functional options
1---2name: go-naming3description: Use when naming any Go identifier — packages, types, functions, methods, receivers, variables, constants, errors, options. Covers MixedCaps, scope-based length, initialism casing, the no-`Get` rule, `-er` interfaces, sentinel `ErrX` vs typed `XError`, and the most commonly missed conventions (constructors, boolean fields, enum zero values, lowercase error strings). Apply proactively whenever new identifiers are introduced, even if the user has not asked about naming.4license: MIT5---67# Go Naming Conventions89Go uses naming to encode visibility (`UpperCamelCase` = exported, `lowerCamelCase` = unexported), so naming is load-bearing — not cosmetic. Names should be **short, contextual, and non-repetitive**. The package name is always present at the call site; pretending otherwise is the single biggest source of bad Go names.1011## Core Rules12131. **MixedCaps only.** No underscores, no `SCREAMING_SNAKE_CASE`, no `kHungarian`. Exceptions: test subtests (`TestFoo_BadInput`), generated code, cgo.142. **Capitalization is visibility.** `Exported`, `unexported`. Do not invent other conventions.153. **No stuttering.** The package name is at the call site; `http.HTTPClient` is wrong, `http.Client` is right.164. **Scope drives length.** `i` is fine in a 3-line loop; package-level vars need descriptive names.175. **Initialisms keep one case.** `userID`, `HTTPServer`, `ParseURL` — never `userId`, `HttpServer`, `ParseUrl`.186. **Receivers are 1-2 letter abbreviations**, consistent across all methods of the type. Never `this`/`self`.1920## Naming Decision Flow2122```23What are you naming?24├─ Package → lowercase single word, singular, specific (not util/common/helper)25├─ File → lowercase, underscores OK (user_handler.go)26├─ Interface → method + "-er" when single-method (Reader, Closer, Stringer)27├─ Struct/Type → MixedCaps noun (Request, FileHeader)28├─ Constructor → New() if package has one primary type; NewThing() if multiple29├─ Constant → MixedCaps; never ALL_CAPS; role-based not value-based30├─ Enum (iota) → type-prefix + Unknown/Invalid at position 031├─ Sentinel error → ErrXxx (var ErrNotFound = errors.New("..."))32├─ Error type → XxxError (type PathError struct{})33├─ Boolean field → is/has/can prefix (isReady, hasPerm)34├─ Getter → field name only (Owner()), never GetOwner()35├─ Setter → SetXxx (SetOwner)36├─ Option → WithXxx (WithLogger, WithPort)37├─ Variant → WithContext suffix, In suffix (in-place), Must prefix (panics)38└─ Variable → length proportional to scope distance39```4041## Quick Reference Table4243| Element | Convention | Example |44|---|---|---|45| Package | lowercase, singular | `http`, `tabwriter` |46| Exported | `UpperCamelCase` | `ReadAll`, `HTTPClient` |47| Unexported | `lowerCamelCase` | `parseToken`, `userCount` |48| Receiver | 1-2 letters | `func (s *Server)` |49| Constant | MixedCaps | `MaxRetries`, `defaultTimeout` |50| Initialism | uniform case | `URL`, `HTTPServer`, `xmlParser` |51| Sentinel error | `Err` prefix | `ErrNotFound` |52| Error type | `Error` suffix | `*PathError` |53| Boolean field | `is`/`has`/`can` | `isConnected` |54| Option func | `With` + field | `WithPort(8080)` |55| Format func | `f` suffix | `Errorf`, `Wrapf` |5657## Frequently Missed Conventions5859These are correct but non-obvious — they account for most naming mistakes in code review.6061### Constructor: `New` vs `NewThing`6263If the package exports **one primary type**, the constructor is `New()`. Callers write `apiclient.New()`, not `apiclient.NewClient()`. Only use `NewThing` when the package builds several things (`http.NewRequest`, `http.NewServeMux`).6465### Boolean Fields Get a Prefix6667Unexported boolean fields use `is`/`has`/`can`. A bare adjective is ambiguous — is `connected` a method or a field, a state or a verb past tense?6869```go70type Conn struct { isOpen bool }71func (c *Conn) IsOpen() bool { return c.isOpen }72```7374### Error Strings Are Fully Lowercase7576Including acronyms. Errors get concatenated: `fmt.Errorf("parsing token: %w", err)` becomes `"parsing token: invalid message id"`. Mid-sentence capitals look wrong. Use `"invalid message id"` not `"invalid message ID"`.7778Sentinel errors should include the package name: `errors.New("apiclient: not found")`.7980### Enum Zero Value Is a Sentinel8182`var s Status` is silently `0`. If `0` is `StatusReady`, uninitialised values look intentional. Put `StatusUnknown` (or `Invalid`) at iota 0.8384```go85type Status int86const (87 StatusUnknown Status = iota // zero-value catch88 StatusReady89 StatusRunning90)91```9293### Subtest Names Are Lowercase Phrases9495```go96t.Run("valid id", ...) // not "Valid ID"97t.Run("empty input", ...)98```99100> Read [references/types-errors-constants.md](references/types-errors-constants.md) when naming new struct/interface/enum/error families.101102## MixedCaps Is Load-Bearing103104```go105MaxPacketSize // good106userCount // good107parseHTTPResponse // good108109MAX_PACKET_SIZE // wrong — Go reserves casing for visibility110max_packet_size // wrong — snake_case111kMaxBufferSize // wrong — Hungarian112```113114## Avoid Stuttering115116The package name is always present at the call site.117118```go119// In package http120type Client struct{} // not HTTPClient — caller writes http.Client121122// In package user123func New() *User // not NewUser — caller writes user.New()124125// In package dbpool126type Pool struct{} // not DBPool127type Option func() // not PoolOption128```129130> Read [references/identifiers-and-scope.md](references/identifiers-and-scope.md) for receivers, variable scope rules, and import aliasing.131132## Avoid Built-In Names133134Never shadow `error`, `string`, `len`, `cap`, `append`, `copy`, `new`, `make`, `nil`, `iota`. The compiler allows it; readers and tools do not.135136## Anti-Patterns137138| Mistake | Fix |139|---|---|140| `MAX_RETRIES = 3` constant | `MaxRetries = 3` — MixedCaps |141| `GetName() string` getter | `Name() string` — Go omits `Get` |142| `HttpClient`, `UserId`, `ParseUrl` | `HTTPClient`, `UserID`, `ParseURL` — uniform initialism case |143| `this`/`self` receiver | One-letter abbreviation (`s` for `Server`) |144| `util`, `common`, `helpers` package | Specific name that describes content (`stringutil`, `httpauth`) |145| `user.NewUser()` constructor | `user.New()` — drop the type name |146| `connected bool` field | `isConnected bool` — prefix reads as a question |147| `"invalid message ID"` error | `"invalid message id"` — fully lowercase |148| `StatusReady` at iota 0 | Add `StatusUnknown` at 0 |149| `userSlice []User` | `users []User` — types do not belong in names |150151## Verification Checklist152153- [ ] No identifier contains `_` outside of test subtests, generated code, or cgo.154- [ ] No `Get` prefix on getters; setters use `Set`.155- [ ] Initialisms are uniform case (grep `Url\|Http\|Json\|Xml\|Id\b` in source).156- [ ] Receivers across one type all use the same short name.157- [ ] All sentinel errors are `ErrXxx`; all error types are `*XxxError`.158- [ ] All iota-based enums place a `Unknown`/`Invalid` value at position 0.159- [ ] No package named `util`, `common`, `helpers`, `misc`.160161## Enforce With Linters162163Most rules are mechanical and a linter will catch them in CI:164165- `revive` — `var-naming`, `exported`, `receiver-naming`, `error-naming`.166- `predeclared` — flags identifiers that shadow built-ins.167- `errname` — enforces `ErrXxx` / `*XxxError`.168- `misspell` — keeps comments and identifiers consistent.169170Add them to `.golangci.yml` and run `golangci-lint run` in CI.171172## References173174- [references/identifiers-and-scope.md](references/identifiers-and-scope.md) — receivers, scope-based length, acronyms, import aliasing175- [references/types-errors-constants.md](references/types-errors-constants.md) — interfaces, structs, enums, sentinel vs typed errors176- [references/functions-and-options.md](references/functions-and-options.md) — constructors, getters, variants, functional options