S
S
svd2222016-09-01 22:43:03
go
svd222, 2016-09-01 22:43:03

package zip golang. Is it possible to "omit" part of the path to the field in structures?

Help me understand the code
here https://golang.org/pkg/archive/zip/#Reader An example is given below for the Reader structure.

// Open a zip archive for reading.
    r, err := zip.OpenReader("testdata/readme.zip")
    if err != nil {
            log.Fatal(err)
    }
    defer r.Close()

    // Iterate through the files in the archive,
    // printing some of their contents.
    for _, f := range r.File {
            fmt.Printf("Contents of %s:\n", f.Name)
            rc, err := f.Open()
            if err != nil {
                    log.Fatal(err)
            }
            _, err = io.CopyN(os.Stdout, rc, 68)
            if err != nil {
                    log.Fatal(err)
            }
            rc.Close()
            fmt.Println()
    }

but the OpenReader method returns a ReadCloser structure and not a Reader, which in turn does not have a File []*zip.File field (Reader does). Also in the sorts https://golang.org/src/archive/zip/reader.go?s=583...
it is declared as
type ReadCloser struct {
    f *os.File
    Reader
}
then
why
r, err := zip.OpenReader("testdata/readme.zip") 

fmt.Printf("%T",r.Reader.File)
fmt.Println("");
fmt.Printf("%T",r.File)
fmt.Println("");

give the same result? Namely:
[]*zip.File

Answer the question

In order to leave comments, you need to log in

1 answer(s)
U
uvelichitel, 2016-09-02
@svd222

The Reader field in the ReadCloser structure is called an unnamed or embedded field in Go, only its type is declared, not its name. The parent struct can use the fields and methods of the injected directly without name qualifiers r.File, but the type name can be used as a qualifier if needed r.Reader.File. Focus does not work with named fields.

type ReadCloser struct {
    f *os.File   //именованное поле f -имя *os.File -тип
    Reader     //внедренное поле Reader -тип имени нет
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question