| name | go |
| description | Go development with gotestsum for testing, standard tooling, and best practices |
Go Development Skill
You are a Go development specialist. This skill provides comprehensive guidance for Go development workflows, testing, and tooling.
Testing Workflow
STRONGLY PREFERRED: Use gotestsum for all test execution.
Running Tests with gotestsum
gotestsum ./...
gt ./...
gotestsum -v ./...
gotestsum -run TestFunctionName ./...
gotestsum ./pkg/mypackage/...
gotestsum --watch ./...
Test Output Formats
gotestsum ./...
gotestsum --format testname ./...
gotestsum --format dots ./...
gotestsum --format pkgname ./...
gotestsum --format standard ./...
gotestsum --format testdox ./...
Common Test Options
gotestsum -race ./...
gotestsum -cover ./...
gotestsum -coverprofile=coverage.out ./...
gotestsum -v ./...
gotestsum -short ./...
gotestsum --fail-fast ./...
gotestsum --count=10 ./...
gotestsum -p 4 ./...
gotestsum -timeout 30s ./...
Watch Mode for TDD
gotestsum --watch ./...
gotestsum --watch ./pkg/mypackage/...
gotestsum --watch --format testdox ./...
Fallback to go test
If gotestsum is not available, use go test:
go test ./...
go test -v ./...
go test -cover ./...
Note: Always prefer gotestsum when available for better output formatting and colors.
Development Workflow
Standard Go Commands
go build ./...
go check ./...
go run ./cmd/myapp
go install ./cmd/myapp
go fmt ./...
gofmt -w .
go vet ./...
Module Management
go mod init github.com/user/project
go get github.com/some/package@v1.2.3
go get -u ./...
go mod tidy
go mod vendor
go mod verify
go mod download
Building
go build
go build -o bin/myapp ./cmd/myapp
go build ./...
GOOS=linux GOARCH=amd64 go build -o bin/myapp-linux ./cmd/myapp
go build -ldflags "-X main.version=1.2.3" ./cmd/myapp
CGO_ENABLED=0 go build -o bin/myapp ./cmd/myapp
Code Quality
go fmt ./...
go vet ./...
staticcheck ./...
golangci-lint run
goimports -w .
Testing Best Practices
Test File Organization
package mypackage_test
package mypackage
Writing Tests
func TestMyFunction(t *testing.T) {
tests := []struct {
name string
input string
want string
wantErr bool
}{
{
name: "valid input",
input: "hello",
want: "HELLO",
},
{
name: "empty input",
input: "",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := MyFunction(tt.input)
if (err != nil) != tt.wantErr {
t.Errorf("MyFunction() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("MyFunction() = %v, want %v", got, tt.want)
}
})
}
}
Test Helpers
func assertNoError(t *testing.T, err error) {
t.Helper()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
}
func TestWithCleanup(t *testing.T) {
tmpDir := setupTempDir(t)
t.Cleanup(func() {
os.RemoveAll(tmpDir)
})
}
Benchmarks
gotestsum -bench=. ./...
go test -bench=. ./...
go test -bench=. -benchmem ./...
go test -bench=BenchmarkMyFunction ./...
Common Workflows
TDD Workflow
gotestsum --watch --format testdox ./...
Pre-commit Checks
go fmt ./... && go vet ./... && gotestsum ./...
go fmt ./... && go vet ./... && gotestsum -race -cover ./...
Coverage Analysis
gotestsum -coverprofile=coverage.out ./...
go tool cover -html=coverage.out
go tool cover -func=coverage.out
go tool cover -func=coverage.out | grep total
Debugging Tests
gotestsum -v -run TestSpecificFunction ./...
gotestsum -v ./... 2>&1 | grep "DEBUG"
dlv test ./pkg/mypackage -- -test.run TestSpecificFunction
Environment Variables
export CGO_ENABLED=0
export GOOS=linux
export GOARCH=amd64
export GOPROXY=https://proxy.golang.org,direct
export GOPRIVATE=github.com/myorg/*
export GOMODCACHE=$HOME/go/pkg/mod
gotestsum Advanced Features
Custom Test Commands
gotestsum -- -tags integration ./...
gotestsum -- -tags "integration e2e" ./...
gotestsum --jsonfile=test-results.json ./...
gotestsum --junitfile=junit.xml ./...
Rerun Failed Tests
gotestsum --rerun-fails ./...
gotestsum --rerun-fails=2 ./...
Post-run Commands
gotestsum --post-run-command='notify-send "Tests done"' ./...
gotestsum --post-run-command='echo "Tests complete" && ./deploy.sh' ./...
Quick Reference
gotestsum ./...
gt ./...
gotestsum --watch ./...
gotestsum -race ./...
gotestsum -cover ./...
gotestsum -v ./...
gotestsum -run TestName ./...
gotestsum --fail-fast ./...
go fmt ./...
go vet ./...
go build ./...
go run ./cmd/myapp
go mod tidy
Best Practices
- Use gotestsum for all testing - Better output, colors, and features
- Write table-driven tests - More maintainable and comprehensive
- Use t.Helper() for test utilities - Clearer test failure locations
- Run tests with -race - Catch race conditions early
- Check coverage - Aim for high test coverage
- Use go fmt - Consistent code formatting
- Run go vet - Catch common mistakes
- Keep tests fast - Use -short flag for quick feedback loops
- Use subtests with t.Run() - Better organization and parallel execution
- Clean up with t.Cleanup() - Proper resource management
CI/CD Integration
gotestsum --format pkgname --junitfile junit.xml -- -race -cover -coverprofile=coverage.out ./...
go tool cover -html=coverage.out -o coverage.html
go tool cover -func=coverage.out | awk '/total:/ {if ($3+0 < 80.0) exit 1}'