Compressing a file

The following example demonstrates how to compress a file using the gzip package:

// This example uses gzip but standard library also // supports zlib, bz2, flate, and lzw package main import ( "compress/gzip" "log" "os" ) func main() { // Create .gz file to write to outputFile, err := os.Create("test.txt.gz") if err != nil { log.Fatal(err) } // Create a gzip writer on top of file writer gzipWriter := gzip.NewWriter(outputFile) defer gzipWriter.Close() // When we write to the gzip writer // it will in turn compress the contents // and then write it to the underlying // file writer as well // We don't have to worry about how all // the compression works since we just // use it as a simple writer interface // that we send ...

Get Security with Go now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.