Golang / GoLang System Architecture and Testing Interview Questions
How do you use test coverage meaningfully in Go — beyond just a percentage?
Test coverage in Go (go test -cover) reports which source lines were executed during tests. But 80% coverage can still miss critical paths. Experienced engineers use coverage to find untested branches, not to chase a number.
// Run coverage // go test -cover ./... // go test -coverprofile=cover.out ./... // go tool cover -html=cover.out # visual HTML report // go tool cover -func=cover.out # per-function percentages // Useful: find UNCOVERED branches visually // Red lines in cover -html are untested paths // Coverage pragmas: mark code as intentionally not testable func panicOnInvariantViolation(cond bool, msg string) { if !cond { // This code path requires a programming error to hit // It's fine to leave untested panic(msg) //nolint:gocritic } } // Testing edge cases found in coverage report func parsePort(s string) (int, error) { n, err := strconv.Atoi(s) if err != nil { return 0, fmt.Errorf("port %q is not a number: %w", s, err) // branch 1 } if n < 1 || n > 65535 { return 0, fmt.Errorf("port %d out of range [1, 65535]", n) // branch 2 } return n, nil // branch 3 } // Table-driven test to hit all branches func TestParsePort(t *testing.T) { tests := []struct { input string want int wantErr bool }{ {"8080", 8080, false}, // happy path (branch 3) {"abc", 0, true}, // not a number (branch 1) {"0", 0, true}, // too low (branch 2) {"65536", 0, true}, // too high (branch 2) {"1", 1, false}, // boundary minimum {"65535", 65535, false}, // boundary maximum } // ... test loop } // CI enforcement: // go test -coverprofile=cover.out ./... // go tool cover -func=cover.out | tail -1 | awk '{print $3}' | grep -v '^[0-6][0-9]'
More Related questions...