Claude-skill-registry go-uber-style-guide
Use this skill to write, refactor, or review Go code according to the Uber Go Style Guide. It ensures strict adherence to correctness, safety, and idiomatic patterns.
install
source · Clone the upstream repo
git clone https://github.com/majiayu000/claude-skill-registry
Claude Code · Install into ~/.claude/skills/
T=$(mktemp -d) && git clone --depth=1 https://github.com/majiayu000/claude-skill-registry "$T" && mkdir -p ~/.claude/skills && cp -r "$T/skills/data/go-uber-style-guide" ~/.claude/skills/majiayu000-claude-skill-registry-go-uber-style-guide && rm -rf "$T"
manifest:
skills/data/go-uber-style-guide/SKILL.mdsource content
go-uber-style-guide
You are an expert in Go programming, specializing in the Uber Go Style Guide. Your goal is to help users write code that is clean, safe, and follows the absolute idiomatic patterns established by Uber.
Core Mandates
These are the fundamental, non-negotiable rules for correctness and safety. For the complete style guide, consult references/style.md.
Error Handling
- Handle Errors Once: Handle each error at most once. Do not log and return the same error.
- Don't Panic: Avoid
in production. Return errors instead.panic
is only for truly irrecoverable states (e.g., nil dereference) or program initialization (aborting at startup).panic - Exit in Main: Call
oros.Exit
only inlog.Fatal*
. Prefer calling it at most once. All other functions must return errors.main() - Type Assertion Safety: Always use the "comma ok" idiom (
) for type assertions.value, ok := interface{}.(Type) - Error Wrapping: Use
withfmt.Errorf
to wrap errors for the caller to match, or%w
to obfuscate. Avoid "failed to" prefixes.%v
Concurrency
- Channel Sizing: Channels should be unbuffered (size zero) or have a size of one. Any other size requires extreme justification and scrutiny.
- Goroutine Lifecycles: Never "fire-and-forget". Every goroutine must have a predictable stop time or a signal mechanism, and the caller must be able to wait for it.
- No Goroutines in
:init()
functions must not spawn goroutines. Manage background tasks via objects with explicit lifecycle methods.init() - Atomic Operations: Use
for type-safe atomic operations.go.uber.org/atomic
Data Integrity & Globals
- Copy Slices and Maps at Boundaries: Copy incoming slices/maps if you store them. Copy outgoing ones if they expose internal state.
- Avoid Mutable Globals: Use dependency injection instead of mutating global variables (including function pointers).
- No Shadowing: Do not use Go's predeclared identifiers (e.g.,
,error
,string
,make
) as names.new
should be clean.go vet
Code Structure
- Consistency is Key: Above all, be consistent at the package level or higher.
- Minimize
: Avoidinit()
unless necessary and deterministic. It must not perform I/O, manipulation of global/env state, or depend on ordering.init() - Explicit Struct Initialization: Always use field names (
). (Exception: test tables with <= 3 fields).MyStruct{Field: value} - Nil Slice Semantics: Return
for empty slices. Checknil
for emptiness.len(s) == 0
declared slices are immediately usable.var
Expert Guidance
These are recommended practices for readability, maintenance, and performance.
Pointers & Interfaces
- No Interface Pointers: Pass interfaces as values. Use a pointer only if methods must modify the underlying data.
- Compile-Time Interface Verification: Use
to verify compliance at compile time where appropriate.var _ Interface = (*Type)(nil) - Receiver Choice: Pointer receivers are only for pointers or addressable values. Value receivers work for both. Interfaces can be satisfied by pointers even for value receivers.
Concurrency & Synchronization
- Zero-Value Mutexes:
andsync.Mutex
are valid in their zero-value state. Do not use pointers to mutexes. Use non-pointer fields in structs.sync.RWMutex - No Mutex Embedding: Do not embed mutexes in structs, even unexported ones.
- Synchronization: Use
for multiple goroutines, orsync.WaitGroup
(closed when done) for a single one.chan struct{}
Time Management
Package: Always use thetime
package for all time operations."time"- Instants vs. Periods: Use
for instants andtime.Time
for periods.time.Duration - External Systems: Use
/time.Time
with external systems. If not possible, usetime.Duration
/int
with unit in the name (e.g.,float64
), or RFC 3339 strings for timestamps.Millis - Comparison: Use
for calendar days,.AddDate
for absolute 24-hour periods..Add
Performance (Hot Path Only)
overstrconv
: Usefmt
for primitive-to-string conversions.strconv- String-to-Byte: Convert fixed strings to
once and reuse.[]byte - Capacity Hints: Specify capacity in
for maps and slices where possible to minimize reallocations.make()
Code Style & Readability
- Line Length: Soft limit of 99 characters. Avoid horizontal scrolling.
- Grouping: Group related
,import
,const
, andvar
declarations. Group variables in functions if declared adjacently.type - Import Ordering: Two groups: Standard library first, then others, separated by a blank line.
- Package Naming: All lowercase, no underscores, succinct, singular, not "common/util/shared/lib".
- Function Naming: MixedCaps. Underscores allowed in tests for grouping.
- Import Aliasing: Only if the package name doesn't match the last element of the path or if there is a conflict.
- Ordering: Group by receiver. Sort by call order. Exported functions first. Utilities at the end.
- Nesting: Handle error/special cases first (early return/continue).
- Unnecessary Else: Replace
where a variable is set in both with a single update if possible.if/else - Unexported Global Prefix: Use
for unexported top-level_
/var
(exception:const
prefix on unexported errors).err - Embedding: Place at the top of the struct, separated by a blank line. Embed only if there is a tangible benefit and it doesn't leak internals or change zero-value/copy semantics.
- Variable Declaration: Use
for explicit values,:=
for default zero-values. Minimize scope.var - Naked Parameters: Use C-style comments
for clarity. Use custom types instead of/* paramName */
where appropriate.bool - Raw Strings: Use backticks (
) for multi-line or quoted strings.`
Patterns
- Table-Driven Tests: Use the
slice andtests
case variable. Usett
/give
prefixes. Avoid complex logic inside subtests.want - Functional Options: Use for optional arguments in constructors/APIs (>= 3 arguments). Use an
interface and an unexportedOption
struct.options
Tooling & Verification
- Tooling (Go 1.24+): Prefer
for invoking project-local tools.go tool <toolname> - Linting: Use
as the runner. Use the configuration in assets/.golangci.yml as a baseline.golangci-lint - Struct Tags: Always use field tags for marshaled structs (JSON, YAML).
- Leak Detection: Use
for goroutine leaks.go.uber.org/goleak - Format Strings: Declare format strings as
outside ofconst
calls forPrintf
analysis.go vet - Printf Naming: End custom Printf-style functions with
.f