我有这个代码。我希望输出:
hello : 1
world : 2
但它输出:
world : 2
world : 2
我的代码有问题吗?
package main
import (
"fmt"
"time"
)
type Task struct {
name string
data int32
}
func (this *Task) PrintData() {
fmt.Println(this.name, ":", this.data)
}
func main() {
tasks := []Task{{"hello", 1}, {"world", 2}}
for _, task := range tasks {
go task.PrintData()
}
time.Sleep(time.Second * 5000)
}
aluckdog
繁花不似锦
相关分类