package main import ( "fmt" "net/http" ) type responseCloser struct { response http.ResponseWriter } func (rc responseCloser) Write(p []byte) (int, error) { return rc.response.Write(p) } func (rc responseCloser) Close() error { return nil } func main() { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { wc := responseCloser{response: w} wc.Write([]byte("Hello, World!")) wc.Close() }) fmt.Println(http.ListenAndServe(":8080", nil)) }
package main import ( "compress/gzip" "fmt" "os" ) func main() { file, err := os.Create("example.txt.gz") if err != nil { fmt.Println(err) } defer file.Close() gzipWriter := gzip.NewWriter(file) defer gzipWriter.Close() wc := io.WriteCloser(gzipWriter) wc.Write([]byte("This example data will be compressed!")) wc.Close() }The io.WriteCloser is part of the standard Go library.