Last active
July 25, 2022 16:05
-
-
Save rsperl/1ab8b17fb0fb1eff1369511f026e9c15 to your computer and use it in GitHub Desktop.
download binary file #go #snippet
This file contains 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
import ( | |
"encoding/binary" | |
"fmt" | |
"io/ioutil" | |
"net/http" | |
"os" | |
) | |
func download() string { | |
client := &http.Client{} | |
req, err := http.NewRequest("GET", downloadURL, nil) | |
resp, err := client.Do(req) | |
if err != nil { | |
fmt.Printf("error downloading new version: %v\n", err) | |
os.Exit(1) | |
} | |
respBody, _ := ioutil.ReadAll(resp.Body) | |
defer resp.Body.Close() | |
filename := tmpdir + "/calibre.img" | |
fh, _ := os.Create(filename) | |
err = binary.Write(fh, binary.LittleEndian, respBody) | |
return filename | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment