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
# 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:
- •
go mod tidy- Clean up and verify dependencies - •
go fmt- Format Go code - •
go vet- Static analysis for suspicious constructs - •
golangci-lint- Comprehensive linting (30+ linters) - •
go test -v -race -cover- Run tests with race detection and coverage - •
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
- •Make code changes - Implement feature or fix
- •Run validation on changed directory (recommended):
bash
bash go-validation/scripts/validate.sh ./path/to/changed/code/
- •Fix any issues - Address failures reported
- •Re-run validation - Ensure all checks pass
- •Optional: Run full project validation:
bash
bash go-validation/scripts/validate.sh
- •Commit - Only commit when validation passes
Common Failures & Quick Fixes
Formatting Errors
Error: go fmt check failed Files not formatted correctly
Fix: Auto-format with --fix flag
bash go-validation/scripts/validate.sh --fix
go vet Errors
Error: go vet failed main.go:15: unreachable code
Fix: Read error message, correct the code issue, re-run validation
Lint Errors
Error: golangci-lint failed error: unused variable 'result'
Fix: Address linter suggestions (remove unused code, check errors, etc.)
Test Failures
Error: Tests failed Expected: 5, Got: 3
Fix: Fix test logic or implementation, ensure all tests pass
Race Conditions
WARNING: DATA RACE Read at 0x00c000102090 by goroutine 7
Fix: Add proper synchronization (mutex, channels, atomic operations)
Coverage Below 80%
Error: Coverage below 80% Current coverage: 65.4%
Fix: Write tests for uncovered code, focus on public APIs and error paths
Security Vulnerabilities
govulncheck: found vulnerabilities Vulnerability in golang.org/x/crypto
Fix: Update vulnerable dependencies to patched versions
Troubleshooting
Validation Script Not Found
# 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
# 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:
- •Individual Commands - Detailed command usage for debugging
- •Troubleshooting Guide - Comprehensive error resolution
- •Testing Best Practices - Test patterns and coverage strategies
- •Security Best Practices - Security guidelines and patterns
Quick Reference
Essential Commands
# 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:
- •Always use the validation script - Never run individual commands
- •Validate frequently - Run during development, not just before commit
- •Use directory-specific validation - Faster feedback loop
- •Leverage auto-fix - Use
--fixflag for formatting issues - •Meet coverage goals - Maintain ≥ 80% test coverage
- •Never commit failing code - All checks must pass
For detailed debugging and advanced topics, see the reference documentation.