This example demonstrates how to integrate goverage into a Go application.
- Go 1.20 or higher
-
Build the application:
./build.sh
-
Run the application:
export GOCOVERDIR=/tmp/coverage-example ./example-app -
Test the endpoints:
# Health check curl http://localhost:8080/api/health # Calculator curl "http://localhost:8080/api/calculate?a=5&b=3"
-
Get coverage data:
curl -X POST http://localhost:7777/v1/cover/profile
Run the complete test workflow:
./test.shThis script will:
- Build the app with coverage
- Start the server
- Exercise the endpoints
- Collect coverage data
- Display the results
The example application includes several code paths:
- Basic HTTP handlers
- Conditional logic in the calculator
- Different calculation scenarios (positive, zero, negative numbers)
By testing different endpoints and parameters, you can see how coverage changes in real-time.
Without coverage:
go build -o example-app .
# Coverage server is NOT included (due to build tags)With coverage:
go build -cover -covermode=atomic -tags=goverage -o example-app .
# Coverage server IS included and starts automatically