runtime: value copy issue #29626
Closed
runtime: value copy issue #29626
Labels
Comments
Please see - https://github.com/golang/go/wiki/CommonMistakes#using-goroutines-on-loop-iterator-variables To fix it, use -
|
yes, I know how to fix it. but pass value by this way, the perfermance maybe a bit effect. |
Then, please try this. func main() {
m := make(map[string]Person, 2)
m["1"] = Person{"n1", 11}
m["2"] = Person{"n2", 12}
for _, v := range m {
v := v // ADD THIS
go output(&v)
}
select {}
} |
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
What version of Go are you using (
go version
)?go 1.10.3 , go 1.10.4 , go 1.11
Does this issue reproduce with the latest release?
yes
What operating system and processor architecture are you using (
go env
)?windows 10 x86_64
What did you do?
package main
import "fmt"
type Person struct {
Name string
Age int
}
func main() {
m := make(map[string]Person, 2)
m["1"] = Person{"n1", 11}
m["2"] = Person{"n2", 12}
for _, v := range m {
go output(&v)
}
select {}
}
func output(v *Person) {
fmt.Println(v)
}
What did you expect to see?
&{n1,11}
&{n2,12}
What did you see instead?
&{n2 12}
&{n2 12}
The text was updated successfully, but these errors were encountered: