Standard library / archive/zip
archive/zip.ReadCloser.Close()
Close the underlying archive file; entry streams must be closed separately. Expected stdout: 'closed\n'.
Formal Syntax & Structure
go syntaxgo
func (rc *ReadCloser) Close() errorReturn Value
error
Nil on success, otherwise operation error.
Working Example
Example Snippetgo
package main
import ( "archive/zip" "bytes" "fmt" "os")
func must(e error) { if e != nil { panic(e) }}func archive() []byte { var b bytes.Buffer w := zip.NewWriter(&b) f, e := w.CreateHeader(&zip.FileHeader{Name: "a.txt", Method: zip.Store}) must(e) _, e = f.Write([]byte("Go")) must(e) must(w.Close()) return b.Bytes()}func main() { dir, e := os.MkdirTemp("", "zip-example-") must(e) defer os.RemoveAll(dir) name := dir + "/a.zip" must(os.WriteFile(name, archive(), 0600)) r, e := zip.OpenReader(name) must(e) must(r.Close()) fmt.Println("closed")}