Last active
September 5, 2022 02:57
-
-
Save Houserqu/e96c0ef66ad4328a1a7578f3c04c9c7f to your computer and use it in GitHub Desktop.
golang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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