Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to display download Progress in go language in Real time

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how to display download progress in real time in go language". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to display download progress in real time in go language".

This example shows how to download files from the Internet to your local computer. By using and passing the response body directly through io.Copy (), we stream the data into a file without having to load it all into memory-small files are not a problem, but they are different when downloading large files.

Package mainimport ("io"net/http"os") func main () {fileUrl: = "http://topgoer.com/static/2/9.png" if err: = DownloadFile (" 9.png ", fileUrl); err! = nil {panic (err)}} / / download file downloads url to a local file, which is written during download rather than loading the entire file into memory. Func DownloadFile (filepath string, url string) error {/ / Get the data resp, err: = http.Get (url) if err! = nil {return err} defer resp.Body.Close () / / Create the file out, err: = os.Create (filepath) if err! = nil {return err} defer out.Close () / / Write the body to file _, err = io.Copy (out, resp.Body) return err}

Download large files with progress bar

The following example is a large file download with a progress bar, to which we pass the response body, io.Copy (), but if you use aQuery TeeReader, you can pass counters to track progress. When downloading, we also save the file as a temporary file, so we will not overwrite the valid file until the file is fully downloaded.

Package mainimport ("fmt"io"net/http"os"strings"github.com/dustin/go-humanize") type WriteCounter struct {Total uint64} func (wc * WriteCounter) Write (p [] byte) (int, error) {n: = len (p) wc.Total + = uint64 (n) wc.PrintProgress () return n, nil} func (wc WriteCounter) PrintProgress () {fmt.Printf ("\ r% s") Strings.Repeat (", 35)) fmt.Printf ("\ rDownloading... " % s complete ", humanize.Bytes (wc.Total)} func main () {fmt.Println (" Download Started ") fileUrl: =" http://topgoer.com/static/2/9.png" err: = DownloadFile ("9.png", fileUrl) if err! = nil {panic (err)} fmt.Println ("Download Finished")} func DownloadFile (filepath string, url string) error {out Err: = os.Create (filepath + ".tmp") if err! = nil {return err} resp, err: = http.Get (url) if err! = nil {out.Close () return err} defer resp.Body.Close () counter: = & WriteCounter {} if _, err = io.Copy (out, io.TeeReader (resp.Body, counter)) Err! = nil {out.Close () return err} fmt.Print ("\ n") out.Close () if err = os.Rename (filepath+ ".tmp", filepath) Err! = nil {return err} return nil} Thank you for your reading. The above is the content of "how to display the download progress in real time in go language". After the study of this article, I believe you have a deeper understanding of how the go language displays the download progress in real time, and the specific usage needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report