Create a io.Reader from a local file

后端 未结 4 1497
悲哀的现实
悲哀的现实 2021-01-31 00:54

I would like to open a local file, and return a io.Reader. The reason is that I need to feed a io.Reader to a library I am using, like:



        
4条回答
  •  不要未来只要你来
    2021-01-31 01:35

    The type *os.File implements the io.Reader interface, so you can return the file as a Reader. But I recommend you to use the bufio package if you have intentions of read big files, something like this:

    file, err := os.Open("path/file.ext")
    // if err != nil { ... }
    
    return bufio.NewReader(file)
    

提交回复
热议问题