Loading...
Loading...
Go naming conventions for packages, functions, methods, variables, constants, and receivers from Google and Uber style guides. Use when naming any identifier in Go code—choosing names for types, functions, methods, variables, constants, or packages—to ensure clarity, consistency, and idiomatic style.
npx skill4agent add cxuu/golang-skills go-namingNormative: Core naming rules (MixedCaps, no underscores) are required per Google's canonical Go style guide. Advisory guidance provides best practices for clarity and maintainability.
Normative: All Go identifiers must use MixedCaps.
MixedCapsmixedCaps// Good
MaxLength // exported constant
maxLength // unexported constant
userID // variable
URLParser // type with initialism
// Bad
MAX_LENGTH // no snake_case
max_length // no underscores
User_Name // no underscores in namesTestFoo_InvalidInputBenchmarkSort_LargeSliceNormative: Packages must be lowercase with no underscores.
tabwritertab_writer// Good: user, oauth2, k8s, tabwriter
// Bad: user_service (underscores), UserService (uppercase), count (shadows var)Advisory: Don't use generic package names.
utilcommonhelpermodelbasestringutilhttpauthconfigloaderimport foopb "path/to/foo_go_proto"foo_pbAdvisory: One-method interfaces use "-er" suffix.
-er// Standard library examples
type Reader interface { Read(p []byte) (n int, err error) }
type Writer interface { Write(p []byte) (n int, err error) }
type Formatter interface { Format(f State, verb rune) }
type CloseNotifier interface { CloseNotify() <-chan bool }ReadWriteCloseStringStringToStringNormative: Receivers must be short abbreviations, used consistently.
| Long Name (Bad) | Better Name |
|---|---|
| |
| |
| |
| |
// Good - consistent short receiver
func (c *Client) Connect() error
func (c *Client) Send(msg []byte) error
func (c *Client) Close() error
// Bad - inconsistent or long receivers
func (client *Client) Connect() error
func (cl *Client) Send(msg []byte) error
func (this *Client) Close() errorNormative: Constants use MixedCaps, never ALL_CAPS or K prefix.
// Good
const MaxPacketSize = 512
const defaultTimeout = 30 * time.Second
// Bad
const MAX_PACKET_SIZE = 512 // no snake_case
const kMaxBufferSize = 1024 // no K prefixAdvisory: Constants should explain what the value denotes.
// Good - names explain the role
const MaxRetries = 3
const DefaultPort = 8080
// Bad - names just describe the value
const Three = 3
const Port8080 = 8080Normative: Initialisms maintain consistent case throughout.
| English | Exported | Unexported |
|---|---|---|
| URL | | |
| ID | | |
| HTTP/API | | |
| gRPC/iOS | | |
// Good: HTTPClient, userID, ParseURL()
// Bad: HttpClient, orderId, ParseUrl()Advisory: Don't useprefix for simple accessors.Get
ownerOwner()GetOwner()SetOwner()// Good
owner := obj.Owner()
if owner != user {
obj.SetOwner(user)
}
// Bad: c.GetName(), u.GetEmail(), p.GetID()ComputeFetchdb.FetchUser(id)stats.ComputeAverage()Advisory: Use noun-like names for getters, verb-like names for actions.
// Noun-like for returning values
func (c *Config) JobName(key string) string
func (u *User) Permissions() []Permission
// Verb-like for actions
func (c *Config) WriteDetail(w io.Writer) errorParseInt()ParseInt64()AppendInt()AppendInt64()Marshal()MarshalText()ivirwusersuserSlicenamenameString_// Good - scope-appropriate naming
for i, v := range items { ... } // small scope
pendingOrders := filterPending(orders) // larger scope
// Good - unexported global with prefix
const _defaultPort = 8080widget.New()widget.NewWidget()p.Name()p.ProjectName()sqldbConnectionDBConnection// Bad - repetitive
func (c *Config) WriteConfigTo(w io.Writer) error
package db
func LoadFromDatabase() error // db.LoadFromDatabase()
// Good - concise
func (c *Config) WriteTo(w io.Writer) error
package db
func Load() error // db.Load()| Element | Rule | Example |
|---|---|---|
| Package | lowercase, no underscores | |
| Exported | MixedCaps, starts uppercase | |
| Unexported | mixedCaps, starts lowercase | |
| Receiver | 1-2 letter abbreviation | |
| Constant | MixedCaps, never ALL_CAPS | |
| Initialism | consistent case | |
| Variable | length ~ scope size | |
go-interfacesgo-style-corego-error-handlinggo-testinggo-defensivego-performance