Created
March 9, 2017 02:55
-
-
Save Xe/9a00369c4085f5fa31d28ba55301534d to your computer and use it in GitHub Desktop.
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" | |
"flag" | |
"log" | |
"os" | |
) | |
// GZippedCSVWriter combines a file, gz writer, and csv writer | |
type GZippedCSVWriter struct { | |
fout *os.File | |
gw *gzip.Writer | |
*csv.Writer | |
} | |
func NewGzippedCSVWriter(fname string) (*GZippedCSVWriter, error) { | |
fout, err := os.Create(fname) | |
if err != nil { | |
return nil, err | |
} | |
gzwr := gzip.NewWriter(fout) | |
csvwriter := csv.NewWriter(gzwr) | |
return &GZippedCSVWriter{ | |
fout: fout, | |
gw: gzwr, | |
Writer: csvwriter, | |
}, nil | |
} | |
func (gz *GZippedCSVWriter) Close() error { | |
gz.Flush() | |
err := gz.gw.Close() | |
if err != nil { | |
return err | |
} | |
err = gz.fout.Close() | |
if err != nil { | |
return err | |
} | |
return nil | |
} | |
var ( | |
fname = flag.String("fname", "", "output fname") | |
) | |
func main() { | |
out, err := NewGzippedCSVWriter(*fname) | |
if err != nil { | |
log.Fatal(err) | |
} | |
out.Write([]string{"doom", "doom", "doom"}) | |
err = out.Close() | |
if err != nil { | |
log.Fatal(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment