All skills
cxuu avatar

/go-naming

@91f0c2e
by Charles Xucxuu/golang-skills165 stars
19

Use when naming any Go identifier — packages, types, functions, methods, variables, constants, or receivers — to ensure idiomatic, clear names. Also use when a user is creating new types, packages, or exported APIs, even if they don't explicitly ask about naming conventions. Does not cover package organization (see go-packages).

Use this Skill: https://skilld.dev/gh/cxuu/golang-skills/go-naming

This session only. Nothing lands on disk.

referencesVARIABLES.md

≈728 tokens on demand. Your agent reads this file only when SKILL.md points to it.

Variable Names

This reference provides detailed guidance on naming variables in Go, covering scope-based naming, single-letter conventions, and avoiding type redundancy.

Length Proportional to Scope

Advisory: Short names for small scopes, longer names for large scopes.

Scope Lines Name Length
Small 1-7 1-2 chars
Medium 8-15 short word
Large 15-25 descriptive
Very large 25+ full words
// Good - short scope, short name
for i := 0; i < len(items); i++ {
    process(items[i])
}

// Good - larger scope, clearer name
func processOrders(orders []*Order) error {
    pendingOrders := filterPending(orders)
    // ... 20+ lines of processing ...
    return nil
}

Single-Letter Variables

Advisory: Use single letters only when meaning is obvious.

Appropriate uses:

  • Loop indices: i, j, k
  • Coordinates: x, y, z
  • Receivers: one or two letters
  • Common types: r for io.Reader, w for io.Writer
  • Short loops: for _, n := range nodes
// Good - familiar conventions
func Copy(w io.Writer, r io.Reader) (int64, error)

for i, v := range values {
    process(v)
}

// Bad - unclear single letters
func Process(a, b, c string) error  // what are a, b, c?

Avoid Type in Variable Name

Advisory: Don't include the type in the variable name.

Repetitive (Bad) Better
var numUsers int var users int
var nameString string var name string
var primaryProject *Project var primary *Project
var userSlice []User var users []User

When disambiguating multiple forms, use meaningful qualifiers:

// Good - meaningful distinction
limitRaw := r.FormValue("limit")
limit, err := strconv.Atoi(limitRaw)

// Also good
limitStr := r.FormValue("limit")
limit, err := strconv.Atoi(limitStr)

Prefix Unexported Globals with _

Source: Uber Go Style Guide

Prefix unexported top-level vars and consts with _ to clarify when they are used that they are global symbols.

Rationale: Top-level variables and constants have package scope. Using a generic name makes it easy to accidentally shadow the value in a different file.

// Bad - hard to distinguish from local variables
const (
    defaultPort = 8080
    defaultUser = "user"
)

func Bar() {
    defaultPort := 9090  // shadows global, no compile error
    fmt.Println("Default port", defaultPort)
}
// Good - clearly global
const (
    _defaultPort = 8080
    _defaultUser = "user"
)

Exception: Unexported error values use the err prefix without underscore:

var errUserNotFound = errors.New("user not found")
var errInvalidInput = errors.New("invalid input")

Source: SKILL.md on GitHub

No alerts17d5 checks · Risk SAFE
  • Gen Agent Trust Hub17d

    This skill provides guidance and a linter script for enforcing idiomatic Go naming conventions. It helps maintain code quality by identifying naming anti-patterns like screaming snake case constants, redundant 'Get' prefixes, and generic package names.

  • Socket17d

    No alerts

  • Snyk17d

    Risk: LOW · No issues

  • Runlayer6mo

    3 files scanned · No issues

  • ZeroLeaks5mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at 91f0c2e. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 2 months ago.

Steadyupdated 3 months ago
What it can do
Runs commands
All 1 allowed tools
Bash(bash:*)

README badge

README badge for cxuu/golang-skills/go-naming