Finish most of Lock things.

This commit is contained in:
zicla
2019-04-25 01:27:30 +08:00
parent f2bed9ac33
commit 53b0af2321
6 changed files with 423 additions and 363 deletions

View File

@ -362,3 +362,37 @@ func DownloadFile(
}
}
// 从指定的url下载一个文件。参考https://golangcode.com/download-a-file-from-a-url/
func HttpDownloadFile(filepath string, url string) (int64, error) {
// Create the file
out, err := os.Create(filepath)
if err != nil {
return 0, err
}
defer func() {
e := out.Close()
PanicError(e)
}()
// Get the data
resp, err := http.Get(url)
if err != nil {
return 0, err
}
defer func() {
e := resp.Body.Close()
PanicError(e)
}()
// Write the body to file
size, err := io.Copy(out, resp.Body)
if err != nil {
return 0, err
}
return size, nil
}