Answer the question
In order to leave comments, you need to log in
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()
}
type ReadCloser struct {
f *os.File
Reader
}
then r, err := zip.OpenReader("testdata/readme.zip")
fmt.Printf("%T",r.Reader.File)
fmt.Println("");
fmt.Printf("%T",r.File)
fmt.Println("");
[]*zip.File
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question