Golang / GoLang Concurrency Mastery Interview Questions
How does GOMAXPROCS=1 change behaviour and when is it actually useful?
With GOMAXPROCS=1, only one goroutine executes at any instant — goroutines interleave cooperatively but never run truly in parallel. This changes the scheduling behaviour but does not eliminate race conditions.
runtime.GOMAXPROCS(1)
// With GOMAXPROCS=1: only one goroutine runs at a time
// There is no true parallelism
// Some races become less likely to manifest — but they still exist!
// WRONG: GOMAXPROCS=1 does NOT eliminate races
// Cooperative preemption at function calls still allows interleaving
var x int
go func() {
x++ // goroutine may yield here
fmt.Println(x) // x may have changed between ++ and Println
}()
// ALWAYS use -race flag — not GOMAXPROCS=1 — to detect races
// Legitimate uses of GOMAXPROCS=1:
// 1. Reproduce bugs that require specific goroutine ordering
// 2. Test that code produces correct results without parallelism
// (if code only works with multiple Ps, that's a design flaw)
// 3. Performance comparison: scheduling overhead vs pure compute
// Testing sequential correctness:
func TestSequential(t *testing.T) {
prev := runtime.GOMAXPROCS(1)
defer runtime.GOMAXPROCS(prev) // restore after test
result := concurrentAlgorithm([]int{1, 2, 3, 4, 5})
if result != 15 {
t.Errorf("expected 15, got %d", result)
}
}
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...
