dongzhiyi2006
2017-10-05 15:34
浏览 109
已采纳

打开文件并在另一个函数中创建NewReader后如何关闭文件?

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!

图片转代码服务由CSDN问答提供 功能建议

我希望func OpenFile()读取gzip文件和bzip2文件。

  func OpenFile(名称字符串)io.Reader {
 
文件,错误:= os.Open(名称)
 
  if err!= nil {
 log.Fatal(err)
} 
 
 if(strings.Contains(name,“ .gz”)){
 
 gzip,gerr:= gzip.NewReader(file  )
 if gerr!= nil {
 log.Fatal(gerr)
} 
返回gzip 
 
}否则if(strings.Contains(name,“ .bz2”)){
 
 bzip2  := bzip2.NewReader(file)
 return bzip2 
 
} else {
返回文件
} 
} 
   
 
 

我称之为OpenFile ()在另一个函数A中:

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

我的问题是,如果我在OpenFile中使用“ defer file.Close()”, ),该文件将过早关闭,因此我无法获得任何输入值。 如何关闭A中的文件?

请注意,gzip.NewReader(file)和bzip2.NewReader(file)返回不同的接口。

gzip:func NewReader(r io.Reader)(* Reader,error)//阅读器具有func Close()

bzip2:func NewReader(r io.Reader)io.Reader // io.Reader没有func Close()

这是我无法在其中返回NewReader(file)的原因 排名第一。

谢谢!

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • doutuo3899 2017-10-05 16:23
    已采纳

    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
        }
    }
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dozr13344 2017-10-05 15:36

    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.

    评论
    解决 无用
    打赏 举报
  • douyiken0968 2017-10-05 15:48

    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.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题