Chapter 9: Naming Rules

Go Variable Naming Rules & Conventions — this is where Go feels very different from Java/Python/C#.

Go has two layers:

  1. Strict syntax rules (from the language spec — what compiles, what doesn’t)
  2. Strong idiomatic conventions (from Effective Go, standard library, community — what makes code feel “Go-like”)

I’ll explain both like your patient teacher, with tables, examples, why each rule exists, and common mistakes.

1. Strict Syntax Rules (Compiler Enforced — Must Follow or It Won’t Compile)

From the official Go spec (https://go.dev/ref/spec#Identifiers — still unchanged in Go 1.26):

  • An identifier (variable name, function name, type name, etc.) is:
    • One or more letters + unicode digits
    • First character must be a letter (includes Unicode letters like హ from Telugu!)
    • Subsequent characters: letters, unicode digits (0-9), or underscore _
  • Cannot be a keyword (if, for, var, func, package, import, etc.)
  • Case-sensitive (Age ≠ age)
  • No spaces, no hyphens, no special chars like @ # $ % ^ & * (except underscore)

Valid examples:

Go

Invalid examples (won’t compile):

Go

Blank identifier _ — special case:

  • You can use _ as a name when you want to ignore a value (especially in multiple returns)
  • But you cannot read from _ (it’s write-only)
Go

2. Idiomatic Naming Conventions (Not Enforced — But Everyone Follows Them)

These come mostly from Effective Go — read this page once, it’s gold.

Key principles:

  • Use MixedCaps or mixedCaps (camelCase) — NO underscores for multi-word names
  • Short is better than long — especially for local variables
  • The closer the name is to its declaration → the shorter it can be
  • Exported (public) names start with Uppercase
  • Unexported (private/package-only) start with lowercase
  • Be descriptive enough — but prefer brevity

Official advice quotes (paraphrased from Effective Go):

“Finally, the convention in Go is to use MixedCaps or mixedCaps rather than underscores to write multiword names.”

“Variable names in Go should be short rather than long. This is especially true for local variables with limited scope. Prefer c to lineCount.”

“The further from its declaration that a name is used, the more descriptive the name must be.”

Quick Style Table for Variables

Scope / Visibility Starting Letter Style Example Recommended Length Common Names / Patterns
Local (inside function) lowercase i, n, err, user, db Very short (1–4 chars) i, j, k (loops), err, ctx, ok, v
Package-level unexported lowercase defaultTimeout, helperFunc Short–medium mu (mutex), once
Exported (public) Uppercase UserID, MaxRetries, HTTPClient Medium–descriptive Config, Logger, DB
Constants Uppercase if exported StatusOK, pi (unexported) Short–medium MaxSize, DefaultPort

Real Examples (Good vs Avoid)

Good idiomatic code:

Go

Avoid (non-idiomatic):

Go

Special Common Short Names (You’ll See Everywhere)

Name Meaning / Use Case Why Short?
i, j, k Loop indices Classic math/CS tradition
n Count / length / size Very common
err Last error returned from call Universal pattern
ok Boolean from map lookup / type assertion value, ok := m[key]
ctx Context.Context (cancellation, deadlines) Standard in modern Go
r http.ResponseWriter or io.Reader Short for reader/writer
w http.ResponseWriter Very common in HTTP handlers
mu sync.Mutex Conventional abbreviation
c Client, Config, Channel, etc. Context-dependent

Your Mini Practice Task

Try rewriting this non-idiomatic code to proper Go style:

Go

Idiomatic version:

Go

Any part unclear?

  • Exported vs unexported in detail?
  • Why no underscores — historical reason?
  • Naming for structs/fields/methods/receivers?
  • Or ready for pointers, slices, or functions next?

Keep going — you’re writing more and more “Go-like” code every day! 💪🇮🇳 Type some code with these rules and feel the difference. 🚀

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *