]> Cypherpunks repositories - gostls13.git/commitdiff
archive/tar: Add reader and writer code example.
authorRobin Eklind <r.eklind.87@gmail.com>
Mon, 4 Feb 2013 01:37:18 +0000 (12:37 +1100)
committerAndrew Gerrand <adg@golang.org>
Mon, 4 Feb 2013 01:37:18 +0000 (12:37 +1100)
Remove the previous comment examples.

R=golang-dev, minux.ma, adg
CC=golang-dev
https://golang.org/cl/7220048

src/pkg/archive/tar/example_test.go [new file with mode: 0644]
src/pkg/archive/tar/reader.go
src/pkg/archive/tar/writer.go

diff --git a/src/pkg/archive/tar/example_test.go b/src/pkg/archive/tar/example_test.go
new file mode 100644 (file)
index 0000000..a5aaadc
--- /dev/null
@@ -0,0 +1,75 @@
+package tar_test
+
+import (
+       "archive/tar"
+       "bytes"
+       "fmt"
+       "io"
+       "log"
+       "os"
+)
+
+func Example() {
+       // Create a buffer to write our archive to.
+       buf := new(bytes.Buffer)
+
+       // Create a new tar archive.
+       tw := tar.NewWriter(buf)
+
+       // Add some files to the archive.
+       var files = []struct {
+               Name, Body string
+       }{
+               {"readme.txt", "This archive contains some text files."},
+               {"gopher.txt", "Gopher names:\nGeorge\nGeoffrey\nGonzo"},
+               {"todo.txt", "Get animal handling licence."},
+       }
+       for _, file := range files {
+               hdr := &tar.Header{
+                       Name: file.Name,
+                       Size: int64(len(file.Body)),
+               }
+               if err := tw.WriteHeader(hdr); err != nil {
+                       log.Fatalln(err)
+               }
+               if _, err := tw.Write([]byte(file.Body)); err != nil {
+                       log.Fatalln(err)
+               }
+       }
+       // Make sure to check the error on Close.
+       if err := tw.Close(); err != nil {
+               log.Fatalln(err)
+       }
+
+       // Open the tar archive for reading.
+       r := bytes.NewReader(buf.Bytes())
+       tr := tar.NewReader(r)
+
+       // Iterate through the files in the archive.
+       for {
+               hdr, err := tr.Next()
+               if err == io.EOF {
+                       // end of tar archive
+                       break
+               }
+               if err != nil {
+                       log.Fatalln(err)
+               }
+               fmt.Printf("Contents of %s:\n", hdr.Name)
+               if _, err := io.Copy(os.Stdout, tr); err != nil {
+                       log.Fatalln(err)
+               }
+               fmt.Println()
+       }
+
+       // Output:
+       // Contents of readme.txt:
+       // This archive contains some text files.
+       // Contents of gopher.txt:
+       // Gopher names:
+       // George
+       // Geoffrey
+       // Gonzo
+       // Contents of todo.txt:
+       // Get animal handling licence.
+}
index ad825c6be0b83339d3da43e6c30324718ff9779f..7467f5c0a4df10dfb178006a1229c81f85a711a4 100644 (file)
@@ -25,20 +25,6 @@ var (
 // A tar archive consists of a sequence of files.
 // The Next method advances to the next file in the archive (including the first),
 // and then it can be treated as an io.Reader to access the file's data.
-//
-// Example:
-//     tr := tar.NewReader(r)
-//     for {
-//             hdr, err := tr.Next()
-//             if err == io.EOF {
-//                     // end of tar archive
-//                     break
-//             }
-//             if err != nil {
-//                     // handle error
-//             }
-//             io.Copy(data, tr)
-//     }
 type Reader struct {
        r   io.Reader
        err error
index 5af504b437d0af0e94495911dd791ba048455174..3325fe3412689275df25f16972935a33c55565d7 100644 (file)
@@ -25,17 +25,6 @@ var (
 // A tar archive consists of a sequence of files.
 // Call WriteHeader to begin a new file, and then call Write to supply that file's data,
 // writing at most hdr.Size bytes in total.
-//
-// Example:
-//     tw := tar.NewWriter(w)
-//     hdr := new(tar.Header)
-//     hdr.Size = length of data in bytes
-//     // populate other hdr fields as desired
-//     if err := tw.WriteHeader(hdr); err != nil {
-//             // handle error
-//     }
-//     io.Copy(tw, data)
-//     tw.Close()
 type Writer struct {
        w          io.Writer
        err        error