Claude-skill-registry go-developer
Instructions for writing Go code following idiomatic Go practices and community standards. Use this when writing or reviewing any code written in Go (Golang).
git clone https://github.com/majiayu000/claude-skill-registry
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-developer" ~/.claude/skills/majiayu000-claude-skill-registry-go-developer && rm -rf "$T"
skills/data/go-developer/SKILL.mdGo Development Instructions
Follow idiomatic Go practices and community standards when writing Go code. These instructions are based on Effective Go, Go Code Review Comments, and Google's Go Style Guide.
General Instructions
- Write simple, clear, and idiomatic Go code
- Favor clarity and simplicity over cleverness
- Follow the principle of least surprise
- Keep the happy path left-aligned (minimize indentation)
- Return early to reduce nesting
- Prefer early return over if-else chains; use
pattern to avoid else blocksif condition { return } - Make the zero value useful
- Write self-documenting code with clear, descriptive names
- Document exported types, functions, methods, and packages
- Use Go modules for dependency management
- Leverage the Go standard library instead of reinventing the wheel (e.g., use
for string concatenation,strings.Builder
for path construction)filepath.Join - Prefer standard library solutions over custom implementations when functionality exists
- Write comments in English by default; translate only upon user request
- Avoid using emoji in code and comments
Naming Conventions
Packages
- Use lowercase, single-word package names
- Avoid underscores, hyphens, or mixedCaps
- Choose names that describe what the package provides, not what it contains
- Avoid generic names like
,util
, orcommonbase - Package names should be singular, not plural
Package Declaration Rules (CRITICAL)
- NEVER duplicate
declarations - each Go file must have exactly ONEpackage
linepackage - When editing an existing
file:.go- PRESERVE the existing
declaration - do not add another onepackage - If you need to replace the entire file content, start with the existing package name
- PRESERVE the existing
- When creating a new
file:.go- BEFORE writing any code, check what package name other
files in the same directory use.go - Use the SAME package name as existing files in that directory
- If it's a new directory, use the directory name as the package name
- Write exactly one
line at the very top of the filepackage <name>
- BEFORE writing any code, check what package name other
- When using file creation or replacement tools:
- ALWAYS verify the target file doesn't already have a
declaration before adding onepackage - If replacing file content, include only ONE
declaration in the new contentpackage - NEVER create files with multiple
lines or duplicate declarationspackage
- ALWAYS verify the target file doesn't already have a
Variables and Functions
- Use mixedCaps or MixedCaps (camelCase) rather than underscores
- Keep names short but descriptive
- Use single-letter variables only for very short scopes (like loop indices)
- Exported names start with a capital letter
- Unexported names start with a lowercase letter
- Avoid stuttering (e.g., avoid
, preferhttp.HTTPServer
)http.Server
Interfaces
- Name interfaces with -er suffix when possible (e.g.,
,Reader
,Writer
)Formatter - Single-method interfaces should be named after the method (e.g.,
→Read
)Reader - Keep interfaces small and focused
Constants
- Use MixedCaps for exported constants
- Use mixedCaps for unexported constants
- Group related constants using
blocksconst - Consider using typed constants for better type safety
Code Style and Formatting
Formatting
- Always use
to format codegofmt - Use
to manage imports automaticallygoimports - Keep line length reasonable (no hard limit, but consider readability)
- Add blank lines to separate logical groups of code
Comments
- Strive for self-documenting code; prefer clear variable names, function names, and code structure over comments
- Write comments only when necessary to explain complex logic, business rules, or non-obvious behavior
- Write comments in complete sentences in English by default
- Translate comments to other languages only upon specific user request
- Start sentences with the name of the thing being described
- Package comments should start with "Package [name]"
- Use line comments (
) for most comments// - Use block comments (
) sparingly, mainly for package documentation/* */ - Document why, not what, unless the what is complex
- Avoid emoji in comments and code
Error Handling
- Check errors immediately after the function call
- Don't ignore errors using
unless you have a good reason (document why)_ - Wrap errors with context using
withfmt.Errorf
verb%w - Create custom error types when you need to check for specific errors
- Place error returns as the last return value
- Name error variables
err - Keep error messages lowercase and don't end with punctuation
Architecture and Project Structure
Package Organization
- Follow standard Go project layout conventions
- Keep
packages inmain
directorycmd/ - Put reusable packages in
orpkg/internal/ - Use
for packages that shouldn't be imported by external projectsinternal/ - Group related functionality into packages
- Avoid circular dependencies
Dependency Management
- Use Go modules (
andgo.mod
)go.sum - Keep dependencies minimal
- Regularly update dependencies for security patches
- Use
to clean up unused dependenciesgo mod tidy - Vendor dependencies only when necessary
Type Safety and Language Features
Type Definitions
- Define types to add meaning and type safety
- Use struct tags for JSON, XML, database mappings
- Prefer explicit type conversions
- Use type assertions carefully and check the second return value
- Prefer generics over unconstrained types; when an unconstrained type is truly needed, use the predeclared alias
instead ofany
(Go 1.18+)interface{}
Pointers vs Values
- Use pointer receivers for large structs or when you need to modify the receiver
- Use value receivers for small structs and when immutability is desired
- Use pointer parameters when you need to modify the argument or for large structs
- Use value parameters for small structs and when you want to prevent modification
- Be consistent within a type's method set
- Consider the zero value when choosing pointer vs value receivers
Interfaces and Composition
- Accept interfaces, return concrete types
- Keep interfaces small (1-3 methods is ideal)
- Use embedding for composition
- Define interfaces close to where they're used, not where they're implemented
- Don't export interfaces unless necessary
Concurrency
Goroutines
- Be cautious about creating goroutines in libraries; prefer letting the caller control concurrency
- If you must create goroutines in libraries, provide clear documentation and cleanup mechanisms
- Always know how a goroutine will exit
- Use
or channels to wait for goroutinessync.WaitGroup - Avoid goroutine leaks by ensuring cleanup
Channels
- Use channels to communicate between goroutines
- Don't communicate by sharing memory; share memory by communicating
- Close channels from the sender side, not the receiver
- Use buffered channels when you know the capacity
- Use
for non-blocking operationsselect
Synchronization
-
Use
for protecting shared statesync.Mutex -
Keep critical sections small
-
Use
when you have many readerssync.RWMutex -
Choose between channels and mutexes based on the use case: use channels for communication, mutexes for protecting state
-
Use
for one-time initializationsync.Once -
WaitGroup usage by Go version:
- If
ingo >= 1.25
, use the newgo.mod
method (documentation):WaitGroup.Go
var wg sync.WaitGroup wg.Go(task1) wg.Go(task2) wg.Wait()- If
, use the classicgo < 1.25
/Add
patternDone
- If
Error Handling Patterns
Creating Errors
- Use
for simple static errorserrors.New - Use
for dynamic errorsfmt.Errorf - Create custom error types for domain-specific errors
- Export error variables for sentinel errors
- Use
anderrors.Is
for error checkingerrors.As
Error Propagation
- Add context when propagating errors up the stack
- Don't log and return errors (choose one)
- Handle errors at the appropriate level
- Consider using structured errors for better debugging
API Design
HTTP Handlers
- Use
for simple handlershttp.HandlerFunc - Implement
for handlers that need statehttp.Handler - Use middleware for cross-cutting concerns
- Set appropriate status codes and headers
- Handle errors gracefully and return appropriate error responses
- Router usage by Go version:
- If
, prefer the enhancedgo >= 1.22net/http
with pattern-based routing and method matchingServeMux - If
, use the classicgo < 1.22
and handle methods/paths manually (or use a third-party router when justified)ServeMux
- If
JSON APIs
- Use struct tags to control JSON marshaling
- Validate input data
- Use pointers for optional fields
- Consider using
for delayed parsingjson.RawMessage - Handle JSON errors appropriately
HTTP Clients
- Keep the client struct focused on configuration and dependencies only (e.g., base URL,
, auth, default headers). It must not store per-request state*http.Client - Do not store or cache
inside the client struct, and do not persist request-specific state across calls; instead, construct a fresh request per method invocation*http.Request - Methods should accept
and input parameters, assemble thecontext.Context
locally (or via a short-lived builder/helper created per call), then call*http.Requestc.httpClient.Do(req) - If request-building logic is reused, factor it into unexported helper functions or a per-call builder type; never keep
(URL params, body, headers) as fields on the long-lived clienthttp.Request - Ensure the underlying
is configured (timeouts, transport) and is safe for concurrent use; avoid mutating*http.Client
after first useTransport - Always set headers on the request instance you’re sending, and close response bodies (
), handling errors appropriatelydefer resp.Body.Close()
Performance Optimization
Memory Management
- Minimize allocations in hot paths
- Reuse objects when possible (consider
)sync.Pool - Use value receivers for small structs
- Preallocate slices when size is known
- Avoid unnecessary string conversions
I/O: Readers and Buffers
-
Most
streams are consumable once; reading advances state. Do not assume a reader can be re-read without special handlingio.Reader -
If you must read data multiple times, buffer it once and recreate readers on demand:
- Use
(or a limited read) to obtainio.ReadAll
, then create fresh readers via[]byte
orbytes.NewReader(buf)
for each reusebytes.NewBuffer(buf) - For strings, use
; you canstrings.NewReader(s)
onSeek(0, io.SeekStart)
to rewind*bytes.Reader
- Use
-
For HTTP requests, do not reuse a consumed
. Instead:req.Body- Keep the original payload as
and set[]byte
before each sendreq.Body = io.NopCloser(bytes.NewReader(buf)) - Prefer configuring
so the transport can recreate the body for redirects/retries:req.GetBodyreq.GetBody = func() (io.ReadCloser, error) { return io.NopCloser(bytes.NewReader(buf)), nil }
- Keep the original payload as
-
To duplicate a stream while reading, use
(copy to a buffer while passing through) or write to multiple sinks withio.TeeReaderio.MultiWriter -
Reusing buffered readers: call
to attach to a new underlying reader; do not expect it to “rewind” unless the source supports seeking(*bufio.Reader).Reset(r) -
For large payloads, avoid unbounded buffering; consider streaming,
, or on-disk temporary storage to control memoryio.LimitReader -
Use
to stream without buffering the whole payload:io.Pipe- Write to
in a separate goroutine while the reader consumes*io.PipeWriter - Always close the writer; use
on failuresCloseWithError(err)
is for streaming, not rewinding or making readers reusableio.Pipe
- Write to
-
Warning: When using
(especially with multipart writers), all writes must be performed in strict, sequential order. Do not write concurrently or out of order—multipart boundaries and chunk order must be preserved. Out-of-order or parallel writes can corrupt the stream and result in errors.io.Pipe -
Streaming multipart/form-data with
:io.Pipe
;pr, pw := io.Pipe()
; usemw := multipart.NewWriter(pw)
as the HTTP request bodypr- Set
toContent-Typemw.FormDataContentType() - In a goroutine: write all parts to
in the correct order; on errormw
; on successpw.CloseWithError(err)
thenmw.Close()pw.Close() - Do not store request/in-flight form state on a long-lived client; build per call
- Streamed bodies are not rewindable; for retries/redirects, buffer small payloads or provide
GetBody
Profiling
- Use built-in profiling tools (
)pprof - Benchmark critical code paths
- Profile before optimizing
- Focus on algorithmic improvements first
- Consider using
for benchmarkstesting.B
Testing
Test Organization
- Keep tests in the same package (white-box testing)
- Use
package suffix for black-box testing_test - Name test files with
suffix_test.go - Place test files next to the code they test
Writing Tests
- Use table-driven tests for multiple test cases
- Name tests descriptively using
Test_functionName_scenario - Use subtests with
for better organizationt.Run - Test both success and error cases
- Consider using
or similar libraries when they add value, but don't over-complicate simple teststestify
Test Helpers
- Mark helper functions with
t.Helper() - Create test fixtures for complex setup
- Use
interface for functions used in tests and benchmarkstesting.TB - Clean up resources using
t.Cleanup()
Security Best Practices
Input Validation
- Validate all external input
- Use strong typing to prevent invalid states
- Sanitize data before using in SQL queries
- Be careful with file paths from user input
- Validate and escape data for different contexts (HTML, SQL, shell)
Cryptography
- Use standard library crypto packages
- Don't implement your own cryptography
- Use crypto/rand for random number generation
- Store passwords using bcrypt, scrypt, or argon2 (consider golang.org/x/crypto for additional options)
- Use TLS for network communication
Documentation
Code Documentation
- Prioritize self-documenting code through clear naming and structure
- Document all exported symbols with clear, concise explanations
- Start documentation with the symbol name
- Write documentation in English by default
- Use examples in documentation when helpful
- Keep documentation close to code
- Update documentation when code changes
- Avoid emoji in documentation and comments
README and Documentation Files
- Include clear setup instructions
- Document dependencies and requirements
- Provide usage examples
- Document configuration options
- Include troubleshooting section
Tools and Development Workflow
Essential Tools
: Format codego fmt
: Find suspicious constructsgo vet
: Additional linting (golint is deprecated)golangci-lint
: Run testsgo test
: Manage dependenciesgo mod
: Code generationgo generate
Development Practices
- Run tests before committing
- Use pre-commit hooks for formatting and linting
- Keep commits focused and atomic
- Write meaningful commit messages
- Review diffs before committing
Common Pitfalls to Avoid
- Not checking errors
- Ignoring race conditions
- Creating goroutine leaks
- Not using defer for cleanup
- Modifying maps concurrently
- Not understanding nil interfaces vs nil pointers
- Forgetting to close resources (files, connections)
- Using global variables unnecessarily
- Over-using unconstrained types (e.g.,
); prefer specific types or generic type parameters with constraints. If an unconstrained type is required, useany
rather thananyinterface{} - Not considering the zero value of types
- Creating duplicate
declarations - this is a compile error; always check existing files before adding package declarationspackage