Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.Sign up
runtime: why a variable in closure can not be read in time? #33591
What version of Go are you using (
Both goroutines are running, but i in main is always 0, and the goroutine that executes i++ does not copy an i to perform the add operation.
If you execute another call in a goroutine that executes i++, such as time.sleep(), the value of i in main will be updated, and another goroutine will get the value of i.
Or you pass &i into these two goroutines, you can also find that the value of i in main is updated, and another goroutine can also get the value of i.
So are these phenomena related to the go compiler or runtime?