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]'
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
