AgentSkillsCN

go-development

以《Effective Go》中的经典模式,深入讲解符合 Go 语言习惯的开发实践。内容涵盖项目结构、用于并发处理的 Goroutine 与通道、显式错误处理、基于表格的测试,以及常见编程惯用法(守卫条款、defer 语句、嵌入式类型)。在编写 Go 服务、CLI 工具或库时,或当用户询问“Go 中该如何处理错误?”或“执行 X 的 Go 式方法是什么?”时,可加以应用。

SKILL.md
--- frontmatter
name: go-development
description: >-
  Covers idiomatic Go development with patterns from Effective Go. Includes
  project structure, goroutines and channels for concurrency, explicit error
  handling, table-driven tests, and common idioms (guard clauses, defer,
  embedding). Use when writing Go services, CLIs, or libraries, or when the user
  asks "how do I handle errors in Go?" or "what's the Go way to do X?"
version: 1.16.0

Go Skill

Patterns and best practices for Go development, grounded in Effective Go principles and community conventions.

Philosophy

Go values simplicity over cleverness. The language deliberately omits features that add complexity without proportional benefit. This isn't a limitation—it's a design decision that produces readable, maintainable code.

Core Principles

  1. Simplicity - If there's a simple way and a clever way, choose simple
  2. Explicitness - Error handling is explicit, not exceptional
  3. Conventions - gofmt settles style debates; follow it
  4. Composition - Prefer embedding and interfaces over inheritance
  5. Concurrency - "Do not communicate by sharing memory; share memory by communicating"

When to Use This Skill

  • Writing Go services, CLIs, or libraries
  • Designing concurrent systems with goroutines and channels
  • Implementing idiomatic error handling
  • Structuring Go projects
  • Writing table-driven tests

Reference Files

FileUse When
references/core.mdSetting up projects, naming variables, formatting code
references/concurrency.mdWorking with goroutines, channels, or sync primitives
references/errors.mdHandling errors, creating custom errors, using panic/recover
references/testing.mdWriting table-driven tests, benchmarks, or examples
references/idioms.mdUsing guard clauses, defer, embedding, or method receivers

Quick Reference

Naming Conventions

ElementConventionExample
Packageslowercase, short, no underscoreshttp, bytes, strconv
ExportedPascalCaseReadFile, HTTPClient
UnexportedcamelCaseparseConfig, internalState
Interfaces (single method)Method + "er"Reader, Stringer, Handler
GettersNo "Get" prefixuser.Name() not user.GetName()
AcronymsAll caps when exportedHTTPServer, xmlParser

Common Patterns

Error check immediately after call:

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

Guard clause (return early):

go
func process(data []byte) error {
    if len(data) == 0 {
        return errors.New("empty data")
    }
    // main logic here
}

Table-driven test:

go
tests := []struct {
    name  string
    input int
    want  int
}{
    {"zero", 0, 0},
    {"positive", 5, 25},
}
for _, tt := range tests {
    t.Run(tt.name, func(t *testing.T) {
        got := Square(tt.input)
        if got != tt.want {
            t.Errorf("Square(%d) = %d, want %d", tt.input, got, tt.want)
        }
    })
}

Core Principles Summary

PrincipleGo Approach
Error handlingReturn errors, check immediately, no exceptions
Formattinggofmt is non-negotiable
ConcurrencyChannels for communication, goroutines are cheap
InterfacesSmall (1-3 methods), defined by consumer
DependenciesStandard library first, minimize external deps
GenericsUse when type safety matters; prefer interfaces when behavior matters

Integration with Foundations

This skill builds on foundations for:

  • Code review practices
  • Testing principles
  • Documentation standards
  • Security considerations

Reference foundations for universal patterns; this skill adds Go-specific idioms.