Zoe Sun
Zoe Sun

Reputation: 89

How to close a file after opening the file and creating a NewReader in another function?

I want the func OpenFile() to read gzip files and bzip2 files. I will add other types later.

func OpenFile(name string) io.Reader{

  file, err := os.Open(name)

  if err != nil {
   log.Fatal(err)
 }

  if(strings.Contains(name, ".gz")){

    gzip, gerr := gzip.NewReader(file)
    if gerr != nil {
        log.Fatal(gerr)
    }
    return gzip

  }else if(strings.Contains(name, ".bz2")){

    bzip2 := bzip2.NewReader(file)
    return bzip2    

  }else{
    return file     
  } 
}

I call the OpenFile() in another function A:

    in := OpenFile(p)

    for _, d := range fdb.Detect(in) {
        set[d] = true
        counter++
    }
    ...

My issue is that if I use "defer file.Close()" in OpenFile(), the file would be closed too early so I can't get any input value. How can I close the file in A?

Notice that the gzip.NewReader(file) and the bzip2.NewReader(file) return different interfaces.

gzip: func NewReader(r io.Reader) (*Reader, error) // Reader has a func Close()

bzip2: func NewReader(r io.Reader) io.Reader // io.Reader doesn't have a func Close()

This is the reason that I can't return NewReader(file) in the first place.

Thank you!

Upvotes: 1

Views: 6405

Answers (3)

Andy Schweig
Andy Schweig

Reputation: 6749

As others have mentioned, you should return an io.ReadCloser from your function. Since the return value of bzip2.NewReader() does not satisfy io.ReadCloser, you'll need to create your own type.

type myFileType struct {
    io.Reader
    io.Closer
}

func OpenFile(name string) io.ReadCloser {

    file, err := os.Open(name)

    if err != nil {
        log.Fatal(err)
    }

    if strings.Contains(name, ".gz") {

        gzip, gerr := gzip.NewReader(file)
        if gerr != nil {
            log.Fatal(gerr)
        }
        return gzip

    } else if strings.Contains(name, ".bz2") {

        bzip2 := bzip2.NewReader(file)
        return myFileType{bzip2, file}

    } else {
        return file
    }
}

Upvotes: 3

John Weldon
John Weldon

Reputation: 40749

In this specific case, because bzip2.NewReader() does not return an io.ReadCloser then the answer by Andy should be the accepted one.

However, my original answer addresses the general case:

You may want to return io.ReadCloser instead of io.Reader - that way the consumer of that function can call Close()

The os.File returned by os.Open() fulfills io.ReadCloser so the only thing to change is the signature (return value) of your OpenFile() function.

Upvotes: 4

Peter
Peter

Reputation: 31691

Returning an io.ReadCloser is idiomatic and the preferred way to do this. It tells the caller that it is expected to call Close when done with the reader.

Another option is to return two arguments, the reader and a close function. That's what context.WithDeadline and context.WithTimeout do:

func OpenFile(name string) (r io.Reader, close func() error) {
    // ...
    var file *os.File
    gzip, _ := gzip.NewReader(file)

    return gzip, func() error { return file.Close() }
}

This may make OpenFile simpler (because you don't have to create any wrapper types), but it is a bit clumsy on the calling side in my opinion.

Upvotes: 3

Related Questions