data := []field{{"one"},{"two"},{"three"}} for _,v := range data { // 解决办法:添加如下语句 // v := v go v.print() } time.Sleep(3 * time.Second) //goroutines print: three, three, three
data2 := []*StructA{{"one"}, {"two"}, {"three"}} // 注意data2是指针数组 StructA 自定义结构 for _, v := range data2 { go v.print() // go执行是函数,函数执行之前,函数的接受对象已经传过来 } time.Sleep(3 * time.Second) //goroutines print: one, two, three