Golang Base Practices

Published by cexll in golang-base-practices-skills

Review recommended55 installs

What this skill does

Go language best practices guide covering frameworks (Gin/Go-Kratos), ORM (GORM), database migrations (Goose), DDD project structure, error handling, concurrency patterns, testing (99% coverage target), and Lint tools. This skill should be used when writing, reviewing, or refactoring Go code to ensure best practices are followed. Triggers on Go project development, code review, performance optimization, or architecture design.

Add Golang Base Practices to your agent

Review the source and files first. When you are ready, copy the prompt instruction or use the CLI command supported by your environment.

Install with a prompt

Paste this into a compatible coding agent:

add this skill "golang-base-practices" from https://github.com/cexll/golang-base-practices-skills

Install with the CLI

Run this command in a controlled environment after reviewing the repository:

npx skills add https://github.com/cexll/golang-base-practices-skills --skill golang-base-practices

Skill instructions

Golang Base Practices

Comprehensive Go development best practices guide, organized by priority for code generation, review, and refactoring. Contains 53 rules referenced from Effective Go, Google Go Style Guide, Uber Go Style Guide, and Go Code Review Comments.

When to Apply

Reference these guidelines when:

  • Creating new Go projects or microservices
  • Building API interfaces (REST/gRPC)
  • Performing database operations and migrations
  • Conducting code reviews and refactoring
  • Optimizing performance and concurrency
  • Improving test coverage

Rule Categories by Priority

PriorityCategoryImpactPrefix
1Framework SelectionCRITICALframework-
2Database & ORMCRITICALdb-
3DDD Project StructureHIGHddd-
4Error HandlingHIGHerror-
5Concurrency PatternsHIGHconcurrency-
6Idiomatic GoMEDIUMidiomatic-
7Testing PracticesCRITICALtesting-
8Performance OptimizationMEDIUMperformance-
9Lint & ToolchainMEDIUMlint-

Quick Reference

1. Framework Selection (CRITICAL)

  • framework-gin-simple - Use Gin for simple projects
  • framework-kratos-complex - Use Go-Kratos for complex microservices
  • framework-middleware - Middleware design patterns
  • framework-graceful-shutdown - Graceful server shutdown

2. Database & ORM (CRITICAL)

  • db-gorm-setup - GORM initialization and configuration
  • db-gorm-hooks - GORM Hook usage guidelines
  • db-gorm-transactions - Transaction handling patterns
  • db-goose-migrations - Database migrations with Goose
  • db-connection-pool - Connection pool configuration

3. DDD Project Structure (HIGH)

  • ddd-project-layout - Standard project directory structure
  • ddd-domain-layer - Domain layer design
  • ddd-application-layer - Application layer design
  • ddd-infrastructure-layer - Infrastructure layer design
  • ddd-interface-layer - Interface layer design
  • ddd-dependency-injection - Dependency injection patterns

4. Error Handling (HIGH)

  • error-wrap-context - Error wrapping with context
  • error-sentinel - Sentinel error definitions
  • error-custom-types - Custom error types
  • error-handling-check - Always check error returns
  • error-api-response - API error response standards
  • error-panic-recover - Panic and recover usage guidelines

5. Concurrency Patterns (HIGH)

  • concurrency-goroutine-lifecycle - Goroutine lifecycle management
  • concurrency-channel-patterns - Channel usage patterns
  • concurrency-channel-size - Channel buffer size selection
  • concurrency-context-cancel - Context cancellation propagation
  • concurrency-errgroup - errgroup concurrency control
  • concurrency-sync-primitives - sync package primitives usage
  • concurrency-race-detection - Race condition detection

6. Idiomatic Go (MEDIUM)

  • idiomatic-naming - Naming conventions
  • idiomatic-comment - Doc Comments guidelines
  • idiomatic-interface - Interface design (prefer small interfaces)
  • idiomatic-receiver - Receiver naming and selection
  • idiomatic-struct-init - Struct initialization
  • idiomatic-functional-options - Functional options pattern
  • idiomatic-defer - defer usage guidelines
  • idiomatic-slice-map - Slice and Map operations
  • idiomatic-zero-value - Zero value utilization
  • idiomatic-embedding - Type embedding
  • idiomatic-blank-identifier - Blank identifier usage

7. Testing Practices (CRITICAL)

  • testing-coverage-99 - 99% test coverage target
  • testing-table-driven - Table-driven tests
  • testing-mock - Mocking and interface abstraction
  • testing-helper - Test helper function guidelines
  • testing-benchmark - Benchmark testing
  • testing-integration - Integration testing standards
  • testing-testify - testify assertion library usage

8. Performance Optimization (MEDIUM)

  • performance-strconv - Use strconv instead of fmt
  • performance-prealloc - Container preallocation

9. Lint & Toolchain (MEDIUM)

  • lint-golangci - golangci-lint configuration
  • lint-gofmt - Code formatting
  • lint-govet - Static analysis
  • lint-staticcheck - Advanced static checking
  • lint-revive - Customizable linter

How to Use

Consult specific rule files in the rules/ directory for detailed explanations and code examples:

rules/framework-gin-simple.md
rules/db-gorm-setup.md
rules/testing-table-driven.md

Each rule file contains:

  • Rule explanation and importance
  • Incorrect example with analysis
  • Correct example with explanation
  • Additional context and references

Core Principles

  1. KISS - Keep it simple, avoid over-engineering
  2. YAGNI - Only implement what is currently needed
  3. Explicit over Implicit - Code intent should be clear
  4. Handle All Errors - Never ignore error returns
  5. 99% Test Coverage - Foundation for high-quality code

Files included

  • SKILL.md
  • rules/concurrency-channel-size.md
  • rules/concurrency-race-detection.md
  • rules/db-goose-migrations.md
  • rules/concurrency-errgroup.md
  • README.md
  • rules/concurrency-channel-patterns.md
  • rules/concurrency-sync-primitives.md
  • rules/concurrency-context-cancel.md
  • rules/db-connection-pool.md
  • rules/concurrency-goroutine-lifecycle.md
  • rules/db-gorm-setup.md
  • rules/db-gorm-hooks.md
  • rules/db-gorm-transactions.md
  • rules/ddd-application-layer.md
  • rules/ddd-dependency-injection.md
  • rules/ddd-infrastructure-layer.md
  • rules/ddd-interface-layer.md
  • rules/ddd-domain-layer.md
  • rules/ddd-project-layout.md
  • rules/error-panic-recover.md
  • rules/error-custom-types.md
  • rules/error-handling-check.md
  • rules/error-sentinel.md
  • rules/framework-gin-simple.md
  • rules/framework-graceful-shutdown.md
  • rules/framework-middleware.md
  • rules/error-wrap-context.md
  • rules/error-api-response.md
  • rules/framework-kratos-complex.md
  • rules/idiomatic-blank-identifier.md
  • rules/idiomatic-comment.md
  • rules/idiomatic-interface.md
  • rules/idiomatic-embedding.md
  • rules/idiomatic-naming.md
  • rules/idiomatic-defer.md
  • rules/idiomatic-functional-options.md
  • rules/idiomatic-receiver.md
  • rules/idiomatic-struct-init.md
  • rules/idiomatic-slice-map.md
  • rules/idiomatic-zero-value.md
  • rules/lint-gofmt.md
  • rules/performance-strconv.md
  • rules/performance-prealloc.md
  • rules/lint-staticcheck.md
  • rules/lint-golangci.md
  • rules/lint-govet.md
  • rules/lint-revive.md
  • rules/testing-benchmark.md
  • rules/testing-helper.md
  • rules/testing-table-driven.md
  • rules/testing-integration.md
  • rules/testing-coverage-99.md
  • rules/testing-mock.md
  • rules/testing-testify.md
Golang Base Practices: Install, Source and Security