go-zhenai-spider icon indicating copy to clipboard operation
go-zhenai-spider copied to clipboard

一个语法上的疑问

Open hxmwr opened this issue 6 years ago • 1 comments

https://github.com/jason-wj/go-zhenai-spider/blob/7dba97719a53f98897471f11b2e3918bfabaa7be/src/crawler/engine/concurrent.go#L40-L43 你好,我也看了这套教程,这里我有点疑问,因为这里是开了 goroutine, 会不会有可能循环执行完了,然后轮到 goroutine 执行时使用了错误的 item 值?

hxmwr avatar Jun 16 '19 13:06 hxmwr

https://github.com/jason-wj/go-zhenai-spider/blob/7dba97719a53f98897471f11b2e3918bfabaa7be/src/crawler/engine/concurrent.go#L40-L43

你好,我也看了这套教程,这里我有点疑问,因为这里是开了goroutine,会不会有可能循环执行完了,然后轮到goroutine执行时使用了错误的项值?

是会有问题的,建议改成这样子:

for _, i := range result.Items {
    go func(result.Items) {
        e.ItemChan <- result.Items[i]
    }(i)
}

bitxx avatar Aug 13 '19 13:08 bitxx