cc-skills-golang

repository·main·Indexed 25 days ago

https://github.com/samber/cc-skills-golang

A guide for developing professional-grade Go CLI applications using the Cobra and Viper stack. The repository provides comprehensive best practices for Go naming conventions (golang-naming v1.0.0), code style (golang-code-style), and data structure implementation (golang-data-structures v1.0.0), covering topics such as functional options, error handling, memory management, and efficient I/O.

Tokens
260.9K
Snippets
620
Records
1.3K
Agent score
81%

What's inside cc-skills-golang

  1. Use samber/do for Generics-Based Dependency Injection

    main

    samber/do provides type-safe dependency injection using Go generics. It avoids reflection and code generation, ensuring that errors are caught at compile time rather than runtime.

    Key features include:

    • No code generation: No extra build steps or generated files.
    • Strongly typed: Uses Go generics to avoid interface{} casting.
    • Built-in lifecycle: Automatically handles health checks and graceful shutdowns for services implementing the Shutdowner interface.
    • Container cloning: Allows creating isolated containers from production configurations for testing.
    • Package system: Enables organizing services by domain.
    // Register services with providers
    injector := do.New()
    do.Provide(injector, func(i do.Injector) (*UserService, error) {
        db := do.MustInvoke[*Database](i)
        return NewUserService(db), nil
    })
    
    // Invoke services (lazy — created on demand)
    svc := do.MustInvoke[*UserService](injector)
    
    // Graceful shutdown — all services implementing Shutdowner are closed
    injector.ShutdownOnSignalsWithContext(ctx, os.Interrupt)
  2. Select an ORM or Database Tool

    main

    Choose your data access layer based on the level of abstraction required:

    • GORM: The most popular feature-complete ORM. Supports associations, hooks, and auto-migrations.
    • SQLx: Provides convenience extensions for database/sql while maintaining performance and type safety.
    • Ent: A code-generated, type-safe entity framework ideal for complex queries and graph traversals.
    • Sqlc: Generates type-safe Go code directly from SQL queries. Uses no runtime reflection and provides compiler-checked queries.
  3. Code Quality Skills

    main

    The following skills cover maintaining high standards in Go codebases:

    • golang-code-style: Formatting, conventions, and project-level style (gofmt, goimports, line length, etc.).
    • golang-documentation: Documentation standards (package docs, godoc, README, CHANGELOG, llms.txt).
    • golang-error-handling: Error creation, wrapping (fmt.Errorf, errors.Is/As), sentinel errors, and panic recovery.
    • golang-lint: Configuring and tuning golangci-lint and interpreting failures.
    • golang-naming: Naming conventions for all identifiers (packages, structs, interfaces, etc.).
    • golang-safety: Defensive coding to prevent panics, nil safety, and runtime bugs.
    • golang-security: Security best practices (injection prevention, cryptography, secrets management).
    • golang-structs-interfaces: Design of structs and interfaces (composition, embedding, struct tags, receivers).
  4. Select Command Line and Configuration Tools

    main

    Build CLI applications and manage configuration:

    • Cobra: The industry standard for building modern CLI applications with subcommands and flags.
    • Viper: Works with Cobra to handle configuration from JSON, YAML, TOML, and environment variables.
    • urfave/cli: A simple and fast alternative to Cobra.
    • Koanf: A lightweight, extensible configuration library supporting multiple formats.
    • env: Parses environment variables directly into Go structs with defaults.
  5. Project Setup & Tooling Skills

    main

    The following skills cover project initialization and lifecycle management:

    • golang-cli: Building CLI applications (project layout, exit codes, signal handling, argument parsing).
    • golang-continuous-integration: Configuring CI/CD pipelines (GitHub Actions).
    • golang-dependency-management: Managing go.mod, versioning, and multi-module workspaces.
    • golang-pkg-go-dev: Exploring packages via godig (pkg.go.dev API client).
    • golang-popular-libraries: Recommendations for production-ready libraries.
    • golang-project-layout: Standard project structures (cmd/, internal/, pkg/).
    • golang-stay-updated: Resources for tracking Go releases and community news.
  6. Architecture & Design Skills

    main

    The following skills cover structural and architectural patterns in Go:

    • golang-concurrency: Goroutines, channels, sync primitives, and patterns like worker pools or fan-out/fan-in.
    • golang-context: Idiomatic usage of context.Context for cancellation, timeouts, and propagation.
    • golang-data-structures: Internal usage of slices, maps, channels, and container/* packages.
    • golang-database: Database access patterns (connection pooling, transactions, migrations, query builders).
    • golang-dependency-injection: DI patterns and library comparisons (wire, dig, fx, do).
    • golang-design-patterns: Idiomatic patterns (functional options, builder pattern, middleware chains).
    • golang-modernize: Using recent language features (iterators, min/max, slog, t.Context).
  7. Select a Web Framework

    main

    Choose a web framework based on your performance and architectural needs:

    • Gin: High-performance, minimalist API. Ideal for REST APIs and microservices.
    • Echo: Minimalist and extensible with a clean middleware system. Suitable for REST APIs and traditional web apps.
    • Fiber: Express.js-inspired and built on Fasthttp. Best for Node.js developers transitioning to Go.
    • Chi: Lightweight, idiomatic router that composes with net/http. Best for small projects with minimal dependencies.