Skip to content

Instantly share code, notes, and snippets.

@Houserqu
Last active September 5, 2022 02:57
Show Gist options
  • Save Houserqu/e96c0ef66ad4328a1a7578f3c04c9c7f to your computer and use it in GitHub Desktop.
Save Houserqu/e96c0ef66ad4328a1a7578f3c04c9c7f to your computer and use it in GitHub Desktop.
golang
func DownloadFile(url string) (localPath string, err error) {
resp, err := http.Get(url)
if err != nil {
panic(err)
}
defer resp.Body.Close()
filename := filepath.Base(url)
localPath = "bg/" + filename
// 创建一个文件用于保存
out, err := os.Create(localPath)
if err != nil {
return
}
defer out.Close()
_, err = io.Copy(out, resp.Body)
if err != nil {
return
}
return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment