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.