Create a io.Reader from a local file

后端 未结 4 1499
悲哀的现实
悲哀的现实 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条回答
  •  Happy的楠姐
    2021-01-31 01:49

    Here is an example where we open a text file and create an io.Reader from the returned *os.File instance f

    package main
    
    import (
        "io"
        "os"
    )
    
    func main() {
        f, err := os.Open("somefile.txt")
        if err != nil {
            panic(err)
        }
        defer f.Close()
    
        var r io.Reader
        r = f
    }
    

提交回复
热议问题