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) } }
More Related questions...