douao3063 2017-06-06 13:28
浏览 14

goroutines问题

I am having trouble understanding channels and goroutines. What I am trying to do is, to continue "paging" until there are no more results(len(matches.Matches) == 0). Instead of breaking out of the infinite loop, my page values run well into the 100000.

func main() {
    results := make(chan []*shodan.HostData)
    var wg sync.WaitGroup


    shodanClient := authorizeShodanApi("redacted")

    page := 0
    for {
        wg.Add(1)
        go func() {
            matches, _ := shodanClient.GetHostsForQuery(&shodan.HostQueryOptions{
                Query: "country:AL port:8080 'HTTP/1.1 200 OK' 'http server 1.0'",
                Page: page,
            })
            defer wg.Done()
            if len(matches.Matches) == 0{
                fmt.Println("DONE")
                return
            }
            results <- matches.Matches
        }()
        page++
    }

    wg.Wait()
}

How can I break the loop as soon as there are no more results?

EDIT: Reason for for{}: Shodan return 100 results per page, but I have no way of knowing how many pages of results there are. Thats why I used a for{} loop, and was hoping to break from the loop as soon as there are no more values (meaning no more pages). page, together with the following code:

matches, _ := shodanClient.GetHostsForQuery(&shodan.HostQueryOptions{
                Query: "country:AL port:8080 'HTTP/1.1 200 OK' 'http server 1.0'",
                Page: page,
            })

Should fire off a request to the shodan API to the specific page. This is also why I would like concurrency.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 如何将下列的“无限压缩存储器”设计出来
    • ¥15 下图接收小电路,谁知道原理
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度
    • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
    • ¥15 ETLCloud 处理json多层级问题
    • ¥15 matlab中使用gurobi时报错
    • ¥15 这个主板怎么能扩出一两个sata口
    • ¥15 不是,这到底错哪儿了😭