Golang http download file / large file source code example

Go http download file / large file source code example

Method 1:
func HttpDownload(url) {
	resp, err := http.Get(url)
	if err != nil {
		return
	}
    defer resp.Body.Close()
	fp, err := os.Create("a")
	if err != nil {
		return
	}
	io.Copy(fp, resp.Body)
}

Method 2:
func HttpDownload(url) {
	resp, err := http.Get(url)
	if err != nil {
		return
	}
    defer resp.Body.Close()
	r := bufio.NewReaderSize(resp.Body, 16 * 1024)
    fp, err := os.Create("a")
    if err != nil {
		return
	}
    w := bufio.NewWriter(fp)
    io.Copy(w, r)
}

Comments

Popular posts from this blog

Python Receiving and parse JSON Data via UDP protocol

ubus lua client method and event registration code demo/example