Why Do Goroutines Print Different Values When Sharing Variables in Go?

Patricia Arquette
Release: 2024-11-05 19:23:02
Original
653 people have browsed it

Why Do Goroutines Print Different Values When Sharing Variables in Go?

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")
}
Copy after login

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")
}
Copy after login

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(&amp;x)
        }()
    }
    wg.Wait()
    fmt.Println("Done")
}
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!