Fundamentals 6 min read

Master Go Naming: Consistent, Concise, and Clear Conventions

This article explains how to choose consistent, short, and precise names in Go, covering general naming principles, the first rule about declaration‑use distance, CamelCase style, local variables, parameters, return values, receivers, exported identifiers, interface naming, error naming, package naming, import paths, and useful standard‑library examples.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master Go Naming: Consistent, Concise, and Clear Conventions

Excellent Naming

Good names should be consistent, short, and precise. Consistency means the same concept is named the same way across different contexts (e.g., avoid using both depend and rely). Shortness prevents the reader from focusing on the name rather than the logic. Precision means the name clearly conveys its meaning.

First Rule

The farther the declaration is from its usage, the longer the name should be.

CamelCase Naming

Go prefers MixedCase (CamelCase) and discourages names_with_underscores. Acronyms should be capitalized, e.g., ServeHTTP, sceneID, CIDRProcessor.

Local Variables

Local variables should be as short as possible, such as buf for buffer or i for index. In very long functions you may need longer names, but that usually signals the function should be refactored.

Parameters

Parameters are like local variables but also serve as documentation. When the type is descriptive, keep the name short:

func AfterFunc(d Duration, f func()) *Timer
func Escape(w io.Writer, s []byte)

If the type is vague, the name should provide documentation.

Return Values

Named return values act like special parameters and can serve as documentation, especially for exported functions.

Method Receivers (Receiver)

Receivers are special parameters that give methods object‑like behavior in Go. By convention, use one or two letters to identify the receiver type.

func (b *Buffer) Read(p []byte) (n int, err error)
func (sh serverHandler) ServeHTTP(rw ResponseWriter, req *Request)
func (r Rectangle) Size() Point

Receiver names should be consistent across methods of the same type.

Exported Package‑Level Naming

When exporting names, avoid repeating the package name. Good examples: bytes.Buffer, strings.Reader. Poor examples: bytes.ByteBuffer, strings.StringReader.

Interface Types

Single‑method interfaces usually end with the er suffix, e.g., Reader. Example:

type Reader interface {
    Read(p []byte) (n int, err error)
}

For multi‑method interfaces, choose a name that precisely describes the purpose, such as net.Conn or http.ResponseWriter.

Error Naming

Error types should be named FooError. Error variables follow the ErrFoo pattern.

type ExitError struct {
    // ...
}

Package Naming

Package names should reflect the exported code they contain and avoid generic names like util or common.

Import Paths

The last segment of an import path must match the package name, be as short as possible, reside at the repository root, and avoid uppercase letters.

Standard Library

Many examples come from the Go standard library, which is a valuable source of inspiration. Remember that even standard‑library authors learn as they write, so occasional mistakes exist.

var ErrFormat = errors.New("unknown format")
func Copy(dst Writer, src Reader) (written int64, err error)
func ScanBytes(data []byte, atEOF bool) (advance int, token []byte, err error)
func Unix(sec, nsec int64) Time
func HasPrefix(s, prefix []byte) bool
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Gobest practicescode stylenaming conventionsprogramming fundamentals
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.