How can I efficiently download a large file using Go?

后端 未结 4 1976
悲哀的现实
悲哀的现实 2021-01-29 18:48

Is there a way to download a large file using Go that will store the content directly into a file instead of storing it all in memory before writing it to a file? Because the fi

4条回答
  •  礼貌的吻别
    2021-01-29 19:08

    1. Here is a sample. https://github.com/thbar/golang-playground/blob/master/download-files.go

    2. Also I give u some codes might help you.

    code:

    func HTTPDownload(uri string) ([]byte, error) {
        fmt.Printf("HTTPDownload From: %s.\n", uri)
        res, err := http.Get(uri)
        if err != nil {
            log.Fatal(err)
        }
        defer res.Body.Close()
        d, err := ioutil.ReadAll(res.Body)
        if err != nil {
            log.Fatal(err)
        }
        fmt.Printf("ReadFile: Size of download: %d\n", len(d))
        return d, err
    }
    
    func WriteFile(dst string, d []byte) error {
        fmt.Printf("WriteFile: Size of download: %d\n", len(d))
        err := ioutil.WriteFile(dst, d, 0444)
        if err != nil {
            log.Fatal(err)
        }
        return err
    }
    
    func DownloadToFile(uri string, dst string) {
        fmt.Printf("DownloadToFile From: %s.\n", uri)
        if d, err := HTTPDownload(uri); err == nil {
            fmt.Printf("downloaded %s.\n", uri)
            if WriteFile(dst, d) == nil {
                fmt.Printf("saved %s as %s\n", uri, dst)
            }
        }
    }
    

提交回复
热议问题