AgentSkillsCN

go-validation

⚠️ 验证时务必使用go-validation/scripts/validate.sh。切勿单独运行go fmt、go vet、golangci-lint或go test。本技能仅提供验证工作流。如需排查验证失败问题,请参阅相关参考资料。

SKILL.md
--- frontmatter
name: go-validation
description: ⚠️ ALWAYS use go-validation/scripts/validate.sh for validation. Never run go fmt, go vet, golangci-lint, or go test individually. This skill provides the validation workflow only. For debugging failures, see reference/.
license: MIT

Go Validation

This skill provides guidance for validating Go code using the comprehensive validation script.

When to Use This Skill

This skill is applicable for:

  • Validating Go code before committing
  • Running comprehensive quality checks
  • Ensuring test coverage meets standards
  • Security vulnerability scanning

⚠️ CRITICAL: Always Use the Validation Script

DO NOT run individual commands (go fmt, go vet, golangci-lint, go test) directly.

The validation script handles everything automatically.

Usage

bash
# Full validation (recommended before commit)
bash go-validation/scripts/validate.sh

# Directory-specific validation (faster feedback during development)
bash go-validation/scripts/validate.sh ./path/to/code/

# Auto-fix formatting issues
bash go-validation/scripts/validate.sh --fix

# With verbose output
bash go-validation/scripts/validate.sh --verbose

What the Script Does

The validation script performs all checks in the correct order:

  1. go mod tidy - Clean up and verify dependencies
  2. go fmt - Format Go code
  3. go vet - Static analysis for suspicious constructs
  4. golangci-lint - Comprehensive linting (30+ linters)
  5. go test -v -race -cover - Run tests with race detection and coverage
  6. govulncheck - Security vulnerability scanning

Validation Requirements

Before considering code complete:

  • All validation checks pass
  • Test coverage ≥ 80%
  • No test failures
  • No race conditions
  • No security vulnerabilities

Validation Workflow

Before Committing

  1. Make code changes - Implement feature or fix
  2. Run validation on changed directory (recommended):
    bash
    bash go-validation/scripts/validate.sh ./path/to/changed/code/
    
  3. Fix any issues - Address failures reported
  4. Re-run validation - Ensure all checks pass
  5. Optional: Run full project validation:
    bash
    bash go-validation/scripts/validate.sh
    
  6. Commit - Only commit when validation passes

Common Failures & Quick Fixes

Formatting Errors

code
Error: go fmt check failed
Files not formatted correctly

Fix: Auto-format with --fix flag

bash
bash go-validation/scripts/validate.sh --fix

go vet Errors

code
Error: go vet failed
main.go:15: unreachable code

Fix: Read error message, correct the code issue, re-run validation

Lint Errors

code
Error: golangci-lint failed
error: unused variable 'result'

Fix: Address linter suggestions (remove unused code, check errors, etc.)

Test Failures

code
Error: Tests failed
Expected: 5, Got: 3

Fix: Fix test logic or implementation, ensure all tests pass

Race Conditions

code
WARNING: DATA RACE
Read at 0x00c000102090 by goroutine 7

Fix: Add proper synchronization (mutex, channels, atomic operations)

Coverage Below 80%

code
Error: Coverage below 80%
Current coverage: 65.4%

Fix: Write tests for uncovered code, focus on public APIs and error paths

Security Vulnerabilities

code
govulncheck: found vulnerabilities
Vulnerability in golang.org/x/crypto

Fix: Update vulnerable dependencies to patched versions

Troubleshooting

Validation Script Not Found

bash
# Navigate to project root
cd /workspace

# Verify script exists
ls -la .github/skills/go-validation/scripts/validate.sh

# Run with bash explicitly
bash go-validation/scripts/validate.sh

Slow Validation

bash
# Validate only changed directory (much faster)
bash go-validation/scripts/validate.sh ./path/to/changed/code/

Need More Details

For detailed information, see the reference documentation:

Quick Reference

Essential Commands

bash
# Full validation
bash go-validation/scripts/validate.sh

# Specific directory
bash go-validation/scripts/validate.sh ./pkg/mypackage/

# Auto-fix
bash go-validation/scripts/validate.sh --fix

Validation Checklist

Before committing:

  • Validation script passes
  • Test coverage ≥ 80%
  • No test failures
  • No race conditions
  • No security vulnerabilities

Summary

Go validation ensures code quality through automated checks:

  1. Always use the validation script - Never run individual commands
  2. Validate frequently - Run during development, not just before commit
  3. Use directory-specific validation - Faster feedback loop
  4. Leverage auto-fix - Use --fix flag for formatting issues
  5. Meet coverage goals - Maintain ≥ 80% test coverage
  6. Never commit failing code - All checks must pass

For detailed debugging and advanced topics, see the reference documentation.