You miss the error returned by out.Close(). So, finish DownloadFile with "return out.Close()" And os.Create will truncate the file, no need to Remove it - though if you want to remove, just "_ = os.Remove(filepath) "- don't check the error.
Gaurav a következőt írta (2022. március 10., csütörtök, 6:54:30 UTC+1): > 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() { > err := DownloadFile(" > https://store1.gofile.io/download/78f1898f-2b02-465a-a39c-4cf7e35822a2/test.zip > ") > if err != nil { > fmt.Println(err) > } > } > > What am I missing here? Regards > Gaurav > > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/c3d2ea0a-5fb3-4791-8715-07978a3dd27fn%40googlegroups.com.