eduardo-sl/go-agent-skills

go-coding-standards

Go coding standards and style conventions grounded in Effective Go, Go Code Review Comments, and production-proven idioms.

查看源码

使用方法

  1. 复制安装命令,在自己的智能体环境安装前先检查源码包。
  2. 从原始文档核对所需工具、凭据和支持的宿主。
  3. 说明输入材料与期望交付物,并对照来源检查输出。
npx skills add https://github.com/eduardo-sl/go-agent-skills --skill go-coding-standards

开始前需了解

  • 安装不包含模型订阅、API 额度或访问文件的权限。
  • 仓库星数和安装量反映活跃情况,不等于独立质量或安全测试。
仓库原始内容

按源仓库内容呈现,保留标题、案例、代码、表格、链接以及原文引用的演示图片。

Go Coding Standards

Idiomatic Go conventions grounded in Effective Go, Go Code Review Comments, and production-proven idioms. All code MUST pass goimports, go vet, and staticcheck (or golangci-lint run) without errors.

1. Import Ordering

Group imports in this order, separated by blank lines:

go
import (
    // 1. Standard library
    "context"
    "fmt"
    "net/http"

    // 2. External packages
    "github.com/gorilla/mux"
    "log/slog"

    // 3. Internal/project packages
    "github.com/myorg/myproject/internal/service"
)

NEVER use dot imports. Use aliasing only to resolve conflicts.

2. Naming Conventions

Packages

  • Short, lowercase, single-word names. No underscores, no camelCase.
  • Name should describe what the package provides, not what it contains.
  • Avoid generic names: util, common, helpers, misc, base.

Functions & Methods

  • MixedCaps (exported) or mixedCaps (unexported). No underscores except in test files.
  • Getters: use Name(), NOT GetName(). Setters: use SetName().
  • Constructors: NewFoo() returns *Foo. If only one type in package: New().

Variables

  • Short names in tight scopes: i, n, err, ctx.
  • Descriptive names for wider scopes: userCount, retryTimeout.
  • Prefix unexported package-level globals with _: var _defaultTimeout = 5 * time.Second.
  • Do NOT shadow built-in identifiers (error, len, cap, new, make, close).

Interfaces

  • Single-method interfaces: method name + -er suffix (Reader, Writer, Closer).
  • Define interfaces where they are consumed, not where they are implemented.

3. Variable Declarations

Top-level

Use var for top-level declarations. Do NOT specify type when it matches the expression:

go
// ✅ Good
var _defaultPort = 8080
var _logger = slog.Default()

// ❌ Bad — redundant type
var _defaultPort int = 8080

Local

  • Prefer := for local variables.
  • Use var only when zero-value initialization is intentional and meaningful.
go
// ✅ Good — zero value is meaningful
var buf bytes.Buffer

// ✅ Good — short declaration
name := getUserName()

4. Struct Initialization

ALWAYS use field names. Never rely on positional initialization:

go
// ✅ Good
user := User{
    Name:  "Alice",
    Email: "alice@example.com",
    Age:   30,
}

// ❌ Bad — positional, breaks on field reordering
user := User{"Alice", "alice@example.com", 30}

Omit zero-value fields unless clarity requires them:

go
// ✅ Good — zero values omitted
user := User{
    Name: "Alice",
}

5. Reduce Nesting

Handle errors and special cases first with early returns. Reduce indentation levels:

go
// ✅ Good — early return
func process(data []Item) error {
    for _, v := range data {
        if !v.IsValid() {
            log.Printf("invalid item: %v", v)
            continue
        }

        if err := v.Process(); err != nil {
            return err
        }

        v.Send()
    }
    return nil
}

Eliminate unnecessary else blocks:

go
// ✅ Good
a := 10
if condition {
    a = 20
}

// ❌ Bad
var a int
if condition {
    a = 20
} else {
    a = 10
}

6. Grouping and Ordering

Group related declarations:

go
const (
    _defaultPort    = 8080
    _defaultTimeout = 30 * time.Second
)

var (
    _validTypes  = map[string]bool{"json": true, "xml": true}
    _defaultUser = User{Name: "guest"}
)

Function ordering within a file:

  1. Constants and variables
  2. New() / constructor functions
  3. Exported methods (sorted by importance, not alphabetically)
  4. Unexported methods
  5. Helper functions

Receiver methods should appear immediately after the type declaration.

7. Line Length

Soft limit of 99 characters. Break long function signatures:

go
func (s *Store) CreateUser(
    ctx context.Context,
    name string,
    email string,
    opts ...CreateOption,
) (*User, error) {

8. Defer Usage

Use defer for cleanup. It makes intent clear at the point of acquisition:

go
mu.Lock()
defer mu.Unlock()

f, err := os.Open(path)
if err != nil {
    return err
}
defer f.Close()

9. Enums

Start enums at 1 (or use explicit sentinel) so zero-value signals "unset":

go
type Status int

const (
    StatusUnknown Status = iota
    StatusActive
    StatusInactive
)

10. Use time Package Properly

  • Use time.Duration for durations, NOT raw integers.
  • Use time.Time for instants. Use time.Since(start) instead of time.Now().Sub(start).
  • External APIs: accept int or float64 and convert internally.
go
// ✅ Good
func poll(interval time.Duration) { ... }
poll(10 * time.Second)

// ❌ Bad
func poll(intervalSecs int) { ... }
poll(10)

11. Receivers

Pick one receiver kind per type and use it for every method on that type. A type with both value and pointer methods has a method set that changes depending on how it is held, which is a bug waiting to happen.

Use a pointer receiver when any of these hold — and then use it everywhere:

  • A method mutates the receiver
  • The struct contains a sync.Mutex or any other field that must not be copied
  • The struct is large enough that copying it per call is measurable
  • Any other method on the type already needs a pointer receiver

Use a value receiver for small immutable types: time.Time-like value objects, enums, and types whose methods only read.

go
// ✅ Consistent — every method on *Buffer takes a pointer
func (b *Buffer) Write(p []byte) (int, error) { ... }
func (b *Buffer) Len() int                     { ... }

// ❌ Mixed — Len() on a value copies the mutex
func (b *Buffer) Write(p []byte) (int, error) { ... }
func (b Buffer) Len() int                      { ... }

Name the receiver one or two letters after the type (s *Server, b *Buffer). Never this or self. Keep the same name across every method on the type.

12. Struct Tags

Tags are strings the compiler does not check. A typo is silent.

go
// ✅ Good — backticks, no spaces after commas, explicit names
type User struct {
    ID        string    `json:"id" db:"id"`
    Email     string    `json:"email" db:"email" validate:"required,email"`
    CreatedAt time.Time `json:"created_at" db:"created_at"`
    password  string    `json:"-"` // never serialised
}

// ❌ Bad
type User struct {
    ID    string `json: "id"`        // space after the colon: tag is ignored
    Email string `json:"email,"`     // trailing comma
    Token string                     // no tag: marshals as "Token"
}

Rules:

  • Tag every field of a type that crosses a serialisation boundary, including

the ones whose name would happen to match.

  • Use json:"-" for anything that must never leave the process. Do not rely

on a field being unexported — an unexported field is skipped by encoding/json silently, which reads as an oversight rather than intent.

  • omitempty omits zero values, so it cannot distinguish "absent" from

"explicitly zero". Use a pointer or a wrapper type when that distinction carries meaning.

  • go vet includes a structtag check. Run it — it catches the malformed

cases above.

Verification Checklist

Before considering code complete:

  1. goimports runs clean
  2. go vet ./... passes
  3. golangci-lint run passes (if configured)
  4. No shadowed built-in identifiers
  5. All imports properly grouped and ordered
  6. Struct initializations use field names
  7. No unnecessary nesting or else blocks
  8. Receiver kind is consistent across every method on a type
  9. Every serialised struct field carries an explicit tag

按任务相关推荐

来自同一仓库

更多 Skills

全部 Skills
eduardo-sl
社区

go-ci

Continuous integration for Go projects: GitHub Actions pipelines, caching, golangci-lint setup, test/coverage gates, vulnerability scanning, build matrices, and Makefile targets. Use when: "set up CI", "GitHub Actions for Go", "add lint to the pipeline", "CI is slow", "coverage gate", "build matrix", "write a Makefile", "run govulncheck in CI". Not for: commit conventions (git-commit), choosing or auditing dependencies (go-dependency-audit), writing the tests themselves (go-test-quality).

安装量
1
GitHub Stars
71
最近更新
8月18日
eduardo-sl
社区

go-cli

Build command-line tools in Go: flag handling, subcommands, stdin/stdout discipline, exit codes, signal handling, and when Cobra/Viper earn their weight over the standard library. Use when: "build a CLI", "add a subcommand", "parse flags", "exit codes", "handle Ctrl+C", "cobra command", "read from stdin", "CLI UX". Not for: HTTP APIs (go-api-design), scaffolding (go-project-layout), service configuration (go-architecture-review).

安装量
1
GitHub Stars
71
最近更新
8月18日
eduardo-sl
社区

go-context

Correct usage of context.Context in Go: propagation, cancellation, timeouts, deadlines, values, and common anti-patterns. Use when: "context usage", "context.Context", "context cancellation", "timeout", "context.WithTimeout", "context.WithCancel", "context values", "context propagation". Not for: concurrency beyond context (go-concurrency-review), HTTP middleware (go-api-design), error handling (go-error-handling).

安装量
1
GitHub Stars
71
最近更新
8月18日
eduardo-sl
社区

go-dependency-injection

Dependency injection in Go: constructor injection, wiring in main, avoiding global state, and when frameworks (wire, fx, dig) earn their complexity. Use when: "dependency injection", "wire up dependencies", "inject this", "remove global state", "singleton in Go", "use google/wire", "uber fx", "make this testable". Not for: interface design (go-interface-design), directory structure (go-project-layout), test doubles and mocks (go-test-quality).

安装量
1
GitHub Stars
71
最近更新
8月18日