mcpField
G
golang

golang

github.com/htlin222Development7838 installsUpdated 3w ago
Repository

About golang

golang is a skill for writing idiomatic Go and reviewing Go code. It covers concurrency, error handling, interfaces, project structure, testing, and module management.

Key features

  1. 01Apply Go concurrency patterns
  2. 02Handle errors explicitly
  3. 03Design small interfaces
  4. 04Organize Go projects
  5. 05Write table-driven tests
  6. 06Manage Go modules

Use cases

  1. 01Adding concurrency to Go code
  2. 02Reviewing Go error handling
  3. 03Designing interfaces for a Go project

Install

Copy the command and run it in a terminal.

$npx skills add https://github.com/htlin222/dotfiles --skill golang

After installing, type/golangin Claude Code to confirm it loaded.

Details

From the project README

Go Development

Write clean, concurrent, idiomatic Go code.

When to Use

  • Writing Go code
  • Concurrency patterns
  • Performance optimization
  • Interface design
  • Go module management

Go Patterns

Concurrency
// Worker pool pattern
func workerPool(jobs <-chan Job, results chan<- Result, workers int) {
    var wg sync.WaitGroup
    for i := 0; i < workers; i++ {
        wg.Add(1)
        go func() {
            defer wg.Done()
            for job := range jobs {
                results <- process(job)
            }
        }()
    }
    wg.Wait()
    close(results)
}

// Context for cancellation
func fetchWithTimeout(ctx context.Context, url string) ([]byte, error) {
    ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
    defer cancel()

    req, _ := http.NewRequestWithContext(ctx, "GET", url, nil)
    resp, err := http.DefaultClient.Do(req)
    if err != nil {
        return nil, err
    }
    defer resp.Body.Close()
    return io.ReadAll(resp.Body)
}
Error Handling
// Custom errors with context
type ValidationError struct {
    Field   string
    Message string
}

func (e *ValidationError) Error() string {
    return fmt.Sprintf("%s: %s", e.Field, e.Message)
}

// Error wrapping
if err != nil {
    return fmt.Errorf("failed to process user %d: %w", userID, err)
}
Interfaces
// Small, focused interfaces
type Reader interface {
    Read(p []byte) error
}

type Writer interface {
    Write(p []byte) error
}

// Composition
type ReadWriter interface {
    Reader
    Writer
}

Project Structure

project/
├── cmd/
│   └── app/
│       └── main.go
├── internal/
│   ├── handler/
│   └── service/
├── pkg/
│   └── utils/
├── go.mod
└── go.sum

Testing

func TestProcess(t *testing.T) {
    tests := []struct {
        name    string
        input   string
        want    string
        wantErr bool
    }{
        {"valid input", "hello", "HELLO", false},
        {"empty input", "", "", true},
    }

    for _, tt := range tests {
        t.Run(tt.name, func(t *testing.T) {
            got, err := Process(tt.input)
            if (err != nil) != tt.wantErr {
                t.Errorf("error = %v, wantErr %v", err, tt.wantErr)
            }
            if got != tt.want {
                t.Errorf("got %v, want %v", got, tt.want)
            }
        })
    }
}

Best Practices

  • Prefer composition over inheritance
  • Keep interfaces small (1-3 methods)
  • Handle errors explicitly
  • Use context for cancellation
  • Avoid global state

Examples

Input: "Add concurrency to this" Action: Identify parallel work, add goroutines with proper sync, handle errors

Input: "Review this Go code" Action: Check error handling, interface design, concurrency safety

Comments

Markdown supported