5 ms·
OnHtml callbacks? Not a big fan of callbacks when you have channels.
by maxpert 9y ago
OnHtml callbacks? Not a big fan of callbacks when you have channels.
- asciimoo 9y agoInteresting idea, how do you imagine a channel based API for this?
- JoeAcchino 9y agoWould 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.
- hellcow 9y agoI 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.
- weberc2 9y agoChannels require concurrency; you have to spin up another goroutine and take care not to let or deadlock. Channels are for communication between goroutines, not for general abstraction.
- hnlmorg 9y agoI agree. Callbacks makes sense in this context and is the idiomatic way to write the code given that's the approach used in the standard library. eg: https://golang.org/pkg/path/filepath/#Walk https://golang.org/pkg/path/filepath/#Walk https://golang.org/pkg/net/http/#HandleFunc https://golang.org/pkg/net/http/#HandleFunc
- weberc2 9y ago*Take care not to leak or deadlock