Hacker News new | ask | show | jobs
by JoeAcchino 3173 days ago
Would this work?

  c := colly.NewCollector()

  // this functions create a goroutine and returns a channel
  ch := c.HTML("a")  
  e := <- ch
  link := e.Attr("href")
  // ...
I'm a bit rusty (ah!) with go, so bear with me if the above contains errors.
2 comments

How do you recognize if the collector has finished? If the site doesn't contain "a" elements (e.g. because of a network error), this example would block forever.
The producer closes the channel. This is differentiable from an open empty channel in a select.
Makes sense, thanks =)

In the above example this would require `nil` checking of the retrieved value every time. I'm not sure if it would make the API cleaner

This would work. No callback hell a pleasure for eyes!