Conventions and best practices for writing readable, maintainable Go code based on the Google Go Style Guide. Use this skill when: Writing new Go packages, services, or command-line tools. Refactoring Go code to improve clarity and maintainability. Enforcing idiomatic Go naming, formatting, and structure. Designing APIs and libraries for long-term maintainability. Avoiding unnecessary abstraction or complexity in Go programs.
This skill implements conventions from the Google Go Style Guide.
The goal is to produce Go code that is:
Readable code is prioritized over clever or complex solutions.
Code must be easy to understand. Prefer self-describing names. Comments should explain why, not what.
Use the simplest approach. Prefer core language features and the standard library. Avoid unnecessary abstraction.
High signal-to-noise ratio. Reduce repetition and boilerplate. Use common Go idioms.
Easy to modify correctly. Clear structure, low coupling, and meaningful tests.
gofmtAll Go source code must be formatted with gofmt.
Imports should be organized into two groups:
Separate these two groups with a blank line. Within each group, imports should be sorted alphabetically.
Example:
import (
"fmt"
"os"
"github.com/google/uuid"
"myproject/internal/pkg"
)
Go uses MixedCaps (camelCase or PascalCase).
Initialisms (acronyms) should have consistent casing. URL should be URL or url, never Url. ID should be ID or id, never Id.
| Correct | Incorrect |
|---|---|
XMLHTTPRequest | XmlHttpRequest |
userID | userId |
appID | appId |
gRPC | grpc |
usercount instead of count).me, this, or self.Example:
func (tr *Tray) Activate() { ... }
func (ri *ResearchInfo) GetDetails() { ... }
Example:
// A Server handles serving quotes from the works of Shakespeare.
type Server struct { ... }
// Encode writes the JSON encoding of req to w.
func Encode(w io.Writer, req *Request) { ... }
Example:
// Good
return fmt.Errorf("something bad happened")
// Bad
return fmt.Errorf("Something bad happened.")
Maintain a comprehensive test suite.
Consistency within a package is paramount. If the style guide is silent, follow the existing patterns in the codebase.