SKILL·FA2C61

go-coding-standards

eduardo-sl
更新于 8 days ago
64
9
64
在 GitHub 上查看
开发general

关于

This skill provides Go style guidance based on Effective Go and Go Code Review Comments for code review and writing tasks. It enforces formatting rules, naming conventions, import ordering, and other idiomatic patterns. Use it specifically for style and formatting checks, not for architecture or performance concerns.

快速安装

Claude Code

推荐
主要方式
npx skills add eduardo-sl/go-agent-skills -a claude-code
插件命令备选方式
/plugin add https://github.com/eduardo-sl/go-agent-skills
Git 克隆备选方式
git clone https://github.com/eduardo-sl/go-agent-skills.git ~/.claude/skills/go-coding-standards

在 Claude Code 中复制并粘贴此命令以安装该技能

技能文档

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:

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:

// ✅ 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.
// ✅ 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:

// ✅ Good
user := User{
    Name:  "Alice",
    Email: "[email protected]",
    Age:   30,
}

// ❌ Bad — positional, breaks on field reordering
user := User{"Alice", "[email protected]", 30}

Omit zero-value fields unless clarity requires them:

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

5. Reduce Nesting

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

// ✅ 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:

// ✅ 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:

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:

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:

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":

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.
// ✅ Good
func poll(interval time.Duration) { ... }
poll(10 * time.Second)

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

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

GitHub 仓库

eduardo-sl/go-agent-skills
路径: skills/(code-quality)/go-coding-standards
0
FAQ

常见问题

什么是 go-coding-standards Skill?

go-coding-standards 是一个 Claude Skill,作者为 eduardo-sl。Skill 将 Claude 按需加载的说明和资源打包,让 Claude 无需额外提示即可执行与 go-coding-standards 相关的任务。

如何安装 go-coding-standards?

使用本页的安装命令:将 go-coding-standards 作为插件添加到 Claude Code,或将其仓库克隆到 skills 目录,然后重启 Claude 以加载该 Skill。

go-coding-standards 属于哪个分类?

go-coding-standards 属于开发分类。

go-coding-standards 可以免费使用吗?

可以。go-coding-standards 已收录在 AIMCP,可免费安装。

相关推荐技能

qmd
开发

这是一个本地搜索和索引的CLI工具,支持BM25、向量搜索和重排序功能。开发者可以用它快速索引本地文件(如Markdown文档)并进行混合搜索,特别适合代码库或文档的本地检索。它还提供MCP模式,能轻松集成到Claude开发环境中使用。

查看技能
subagent-driven-development
开发

该Skill用于在当前会话中执行包含独立任务的实施计划,它会为每个任务分派一个全新的子代理并在任务间进行代码审查。这种"全新子代理+任务间审查"的模式既能保障代码质量,又能实现快速迭代。适合需要在当前会话中连续执行独立任务,并希望在每个任务后都有质量把关的开发场景。

查看技能
mcporter
开发

mcporter Skill 让开发者能在Claude中直接管理和调用MCP服务器。它支持列出可用服务器、调用工具、处理OAuth认证以及管理服务器守护进程。开发者可以通过命令行式交互快速执行`mcporter list`查看服务器,或使用`mcporter call`直接调用工具,简化了MCP工作流程。

查看技能
adk-deployment-specialist
开发

这是一个用于部署和编排Google Vertex AI ADK智能体的Claude Skill,专为构建生产级多智能体系统而设计。它支持通过A2A协议进行智能体通信,提供代码执行沙箱和记忆库功能,并能处理智能体发现与任务提交。当开发者需要部署ADK智能体或编排多智能体协作时,可使用此Skill来简化Vertex AI Agent Engine的部署流程。

查看技能