Goroutine Shared Variable Behavior in Go
Question:
While exploring Go's concurrency features, a developer encountered an unexpected behavior when sharing variables between goroutines. In the presence of certain code modifications, the outputs starkly differed.
In the below code snippet, each goroutine correctly prints its corresponding x value:
package main import ( "fmt" "sync" ) func main() { var wg sync.WaitGroup for i := 0; i < 5; i++ { wg.Add(1) x := i go func() { defer wg.Done() fmt.Println(x) }() } wg.Wait() fmt.Println("Done") }
However, a slight modification to the code produces a uniform result for all goroutines:
package main import ( "fmt" "sync" ) func main() { var wg sync.WaitGroup for i := 0; i < 5; i++ { wg.Add(1) go func() { defer wg.Done() fmt.Println(i) }() } wg.Wait() fmt.Println("Done") }
Answer:
In the first snippet, each goroutine has a fresh instance of the variable x. This is because x := i creates a new variable with every loop iteration.
To demonstrate this, we can print the memory address of x inside each goroutine:
package main import ( "fmt" "sync" ) func main() { var wg sync.WaitGroup for i := 0; i < 5; i++ { wg.Add(1) x := i go func() { defer wg.Done() fmt.Println(&x) }() } wg.Wait() fmt.Println("Done") }
Running this code will show different memory addresses for each goroutine.
In the second snippet, the variable i is directly referenced in the anonymous function passed to go func(). This means that all goroutines share the same i value, which accounts for the uniform output.
The above is the detailed content of Why Do Goroutines Print Different Values When Sharing Variables in Go?. For more information, please follow other related articles on the PHP Chinese website!