file, err := os.Create("output.txt") if err != nil { log.Fatal(err) } defer file.Close() writeCloser := bufio.NewWriter(file) defer writeCloser.Close() writeCloser.WriteString("Hello World!\n") writeCloser.Flush()
file, err := os.Create("output.gz") if err != nil { log.Fatal(err) } defer file.Close() gzWriter := gzip.NewWriter(file) defer gzWriter.Close() writeCloser := bufio.NewWriter(gzWriter) defer writeCloser.Close() writeCloser.WriteString("Hello World!\n") writeCloser.Flush()In this example, we create a new gzip file named `output.gz` and create a `gzip.Writer` out of it. We create a new `bufio.Writer` out of the `gzip.Writer` and write a string to the `WriteCloser`. We then call the `Flush()` method to ensure that all the data is written to the gzip file. Finally, we close the `WriteCloser` which closes the underlying gzip file. Package: `io`