38 lines
548 B
Go
38 lines
548 B
Go
|
package download
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"io"
|
||
|
"net/http"
|
||
|
"os"
|
||
|
)
|
||
|
|
||
|
func ByURL(url, location string) (err error) {
|
||
|
// Create the file
|
||
|
out, err := os.Create(location)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
defer out.Close()
|
||
|
|
||
|
// Get the data
|
||
|
resp, err := http.Get(url)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
defer resp.Body.Close()
|
||
|
|
||
|
// Check server response
|
||
|
if resp.StatusCode != http.StatusOK {
|
||
|
return fmt.Errorf("bad status: %s", resp.Status)
|
||
|
}
|
||
|
|
||
|
// Writer the body to file
|
||
|
_, err = io.Copy(out, resp.Body)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|