hmm/fetch.go

35 lines
603 B
Go
Raw Normal View History

2017-05-12 13:44:54 -07:00
package hmm
import (
"bufio"
2017-06-30 16:13:31 -07:00
"io"
"io/ioutil"
2017-05-12 13:44:54 -07:00
"net/http"
)
// Lines returns a chan containing one line of output from the remote url.
2017-05-12 13:49:54 -07:00
//
// Lines closes the internal chan after it's fetched all bytes. Errors are not
// propagated.
2017-05-12 13:44:54 -07:00
func Lines(url string) <-chan string {
r := make(chan string)
go func() {
defer close(r)
resp, err := http.Get(url)
if err != nil {
return
}
s := bufio.NewScanner(resp.Body)
for s.Scan() {
r <- s.Text()
}
// ignore errors
2017-06-30 16:13:31 -07:00
// if resp.StatusCode != http.StatusOK {}
io.Copy(ioutil.Discard, resp.Body)
resp.Body.Close()
2017-05-12 13:44:54 -07:00
}()
return r
}