O'Reilly logo

Go Standard Library Cookbook by Radomir Sohlich

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How to do it...

  1. Open the console and create the folder chapter05/recipe11.
  2. Navigate to the directory.
  3. Create the zip.go file with the following content:
        package main        import (          "archive/zip"          "bytes"          "fmt"          "io"          "io/ioutil"          "log"          "os"        )        func main() {          var buff bytes.Buffer          // Compress content          zipW := zip.NewWriter(&buff)          f, err := zipW.Create("newfile.txt")          if err != nil {            panic(err)          }          _, err = f.Write([]byte("This is my file content"))          if err != nil {            panic(err)          }          err = zipW.Close()          if err != nil {            panic(err)          }          //Write output to file          err = ioutil.WriteFile("data.zip", buff.Bytes(), os.ModePerm)          if err != nil {            panic(err)          }          // Decompress the content          zipR, err := zip.OpenReader("data.zip")          if err != nil {            panic(err)          } for _, file := range zipR.File ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required