Files
PandaX/base/file/file.go
2022-01-14 17:37:01 +08:00

34 lines
611 B
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package utilFile
import (
"io"
"net/http"
"os"
)
/**
* @Description 添加qq群467890197 交流学习
* @Author 熊猫
* @Date 2022/1/14 11:13
**/
// DownloadFile 会将url下载到本地文件它会在下载时写入而不是将整个文件加载到内存中。
func DownloadFile(url, filepath 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
}