I am trying to download a zip file using a simple http Get. This program downloads the file but the downloaded file is corrupted.
package main
import (
"fmt"
"io"
"net/http"
"os"
)
func DownloadFile(url string) error {
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
filepath := "test.zip"
_, err = os.Stat(filepath)
if err == nil {
err := os.Remove(filepath)
if err != nil {
return err
}
}
out, err := os.Create(filepath)
if err != nil {
return err
}
defer out.Close()
_, err = io.Copy(out, resp.Body)
if err != nil {
return err
}
return nil
}
func main() {
if err != nil {
fmt.Println(err)
}
}
What am I missing here?
Regards
Gaurav