top | item 15408928

(no title)

asciimoo | 8 years ago

Interesting idea, how do you imagine a channel based API for this?

discuss

order

hellcow|8 years ago

I would ignore the GP's advice. Channels are prone to big errors -- panics and blocking -- which aren't detectable at compile time. They make sense to use internally but shouldn't be exposed in a public API. As one example, notice how the standard library's net/http package doesn't require you to use channels, but it uses them internally.

JoeAcchino|8 years 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.

asciimoo|8 years ago

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.

maxpert|8 years ago

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