Last active
March 3, 2016 00:06
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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