This is a created article in which the information may have evolved or changed.
Golang Language co-process
When using global variables, local variables, pointers, maps, slices, etc. as parameters in the process, it is important to note that the value of this variable is changing. With a For loop, use more caution.
1, built-in functions directly using local variables, no parameter passing
Func Main () {
For I: = 0; I < 100; i++ {
Go func () {
Fmt. Println (i)
}()
}
}
Run effect
Func Main () {
For I: = 0; I < 100; i++ {
Go func (i int) {
Fmt. Println (i)
} (i)
}
}
Run effect
2. Parameters for address passing
Type Per struct {
Name string
BH INT
}
Func Main () {
P: = &per{}
For I: = 0; I < 100; i++ {
P.BH = i
Go func (P *per) {
Fmt. PRINTLN (P)
} (P)
}
}
Analysis: The reason for this non-actual result is that it does not start the process immediately after go.