Go Naming Conventions
Beginner
Follow Go's naming conventions strictly — MixedCaps for exports, mixedCaps for unexported, short receiver names, acronym casing, and meaningful package names without stuttering.
File Patterns
**/*.go**/go.mod
This rule applies to files matching the patterns above.
Rule Content
rule-content.md
# Go Naming Conventions
## Rule
Follow Go's official naming conventions: MixedCaps for exported identifiers, mixedCaps for unexported, short meaningful names, and no package name stuttering.
## Naming Rules
| Element | Convention | Example |
|---------|-----------|---------|
| Exported | MixedCaps | `HTTPClient`, `UserService` |
| Unexported | mixedCaps | `httpClient`, `userCount` |
| Package | lowercase, single word | `http`, `user`, `auth` |
| Interface (single method) | Method + "er" | `Reader`, `Stringer` |
| Receiver | 1-2 letter abbreviation | `(s *Server)`, `(u *User)` |
| Acronyms | All caps | `ID`, `HTTP`, `URL`, `API` |
## Good Examples
```go
package user
// Exported types — MixedCaps, no package stuttering
type Service struct { ... } // user.Service (not user.UserService)
type Repository interface { ... } // user.Repository
// Short receiver names
func (s *Service) Create(ctx context.Context, u *User) error {
// ...
}
// Acronyms in all caps
type HTTPClient struct {
baseURL string
apiKey string
userID string
}
// Interface naming
type Reader interface { Read(p []byte) (n int, err error) }
type Validator interface { Validate() error }
// Local variables — short, contextual
for i, v := range items { ... }
for _, u := range users { ... }
```
## Bad Examples
```go
// BAD: Package name stuttering
package user
type UserService struct { ... } // user.UserService — redundant
type UserRepo struct { ... } // user.UserRepo — redundant
// BAD: Wrong acronym casing
type HttpClient struct { ... } // Should be HTTPClient
type UserId string // Should be UserID
type ApiKey string // Should be APIKey
// BAD: Long receiver names
func (service *UserService) GetUser() { ... } // Should be (s *Service)
func (this *Server) Handle() { ... } // Never use "this" or "self"
// BAD: Underscore in names
var user_name string // Should be userName
func get_user() { ... } // Should be getUser
```
## Enforcement
- `gofmt` enforces formatting but not naming
- `golangci-lint` with revive rules catches naming issues
- Code review: verify package names do not stutter with type namesFAQ
Discussion
Loading comments...