Skip to content

Instantly share code, notes, and snippets.

@mhfs
Last active March 3, 2016 00:06
Show Gist options
  • Save mhfs/12272814bf8a3145435a to your computer and use it in GitHub Desktop.
Save mhfs/12272814bf8a3145435a to your computer and use it in GitHub Desktop.
How to stream, uncompress and decode a CSV file straight from a remote URL.
package main
import (
"compress/gzip"
"encoding/csv"
"fmt"
"io"
"net/http"
"os"
)
func main() {
resp, err := http.Get(os.Getenv("FILE_URL"))
if err != nil {
panic("couldn't read from url")
}
gzipReader, err := gzip.NewReader(resp.Body)
if err != nil {
panic("couldn't create gzip reader")
}
csvReader := csv.NewReader(gzipReader)
for {
record, err := csvReader.Read()
if err == io.EOF {
break
} else if err != nil {
panic("Error reading csv record: " + err.Error())
}
// TODO do whatever you need with the record
fmt.Println(record)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment