I’m building my own docker client, and I stumble on something very basic, I think. It might be of my own shaky understanding of how GO handles IO streams, or what not… I’m assuming I’m missing something very basic that you guys can help me with:
I’m writting the equivalent of docker load
here, where I can handle multiple formats of tarballs (tar.gz, tar, tar.bz2, tar.xz). I’m having issues on how I deal with those multiple formats.
The main “loader” function reads as this (for now) :
func LoadImage(filepaths []string) {
ctx := context.Background()
cli := hosts.Connect2daemon(true)
for _, filepath := range filepaths {
tarballFile, err := os.Open(filepath)
if err != nil {
fmt.Println("Opening tarball file error: ", err)
os.Exit(-9)
}
defer tarballFile.Close()
tarballFormatReader := getTarballCompressedFormatReader(filepath, tarballFile)
if tarballFormatReader == nil {
fmt.Println("Unknown file format, exiting")
os.Exit(-10)
}
defer tarballFormatReader.Close()
tarReader := tar.NewReader(tarballFormatReader)
}
}
As you see, it calls on getTarballCompressedFormatReader
, which reads like:
func getTarballCompressedFormatReader(tbname string, tbfile *os.File) io.Reader {
if strings.HasSuffix(tbname, ".bz2") {
return bzip2.NewReader(tbfile)
}
if strings.HasSuffix(tbname, ".xz") {
reader, _ := xz.NewReader(tbfile)
return reader
}
if strings.HasSuffix(tbname, ".gz") {
reader, _ := gzip.NewReader(tbfile)
return reader
}
if strings.HasSuffix(tbname, ".tar") {
return tar.NewReader(tbfile)
}
return nil
}
The issue is on the on the defer tarballFormatReader.Close()
line, where it complains of having no method for Close(). I do not get it, it should, doesn’t it ?
This is my first full-scale software in GO, so I might be missing some basic understanding…