Golang / GoLang System Architecture and Testing Interview Questions
How does Go's built-in fuzzing work and when should you use property-based testing?
Go 1.18 added native fuzz testing via go test -fuzz. Fuzzing automatically generates inputs that exercise edge cases your hand-written tests miss — particularly effective for parsers, serialisers, and cryptographic code.
// Fuzz test — finds inputs that cause a panic or incorrect result
func FuzzParseURL(f *testing.F) {
// Seed corpus: known interesting inputs
f.Add("https://example.com/path?q=1")
f.Add("http://user:pass@host:8080/")
f.Add("")
f.Fuzz(func(t *testing.T, s string) {
// Property: parsing should never panic
u, err := url.Parse(s)
if err != nil { return } // error is ok, panic is not
// Property: round-trip should be stable
// parsing the string representation should give the same URL
u2, err := url.Parse(u.String())
if err != nil {
t.Errorf("round-trip parse failed: %v", err)
}
if u.String() != u2.String() {
t.Errorf("round-trip changed URL: %q → %q",
u.String(), u2.String())
}
})
}
// Another fuzz example: JSON encode/decode round-trip
func FuzzJSONRoundTrip(f *testing.F) {
f.Add(`{"name":"Alice","age":30}`)
f.Fuzz(func(t *testing.T, data []byte) {
var v map[string]any
if err := json.Unmarshal(data, &v); err != nil { return }
encoded, err := json.Marshal(v)
if err != nil {
t.Errorf("marshal failed after successful unmarshal: %v", err)
}
var v2 map[string]any
if err := json.Unmarshal(encoded, &v2); err != nil {
t.Errorf("second unmarshal failed: %v", err)
}
})
}
// Run fuzzing:
// go test -fuzz=FuzzParseURL # fuzz until stopped
// go test -fuzz=FuzzParseURL -fuzztime=60s # fuzz for 60 seconds
// go test # replays corpus only (CI)When to use fuzzing: parsers (JSON, YAML, protobuf), network protocol handlers, cryptographic code, regular expression engines, any function that accepts arbitrary byte/string input. Fuzzing found critical bugs in Go's own standard library.
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...
