Golang download image from given URL
Example
package main
import (
"fmt"
"io"
"net/http"
"net/url"
"os"
"strings"
)
var (
fileName string
fullUrlFile string
)
func main() {
fullUrlFile = "http://www.golangprograms.com/skin/frontend/base/default/logo.png"
// Build fileName from fullPath
buildFileName()
// Create blank file
file := createFile()
// Put content on file
putFile(file, httpClient())
}
func putFile(file *os.File, client *http.Client) {
resp, err := client.Get(fullUrlFile)
checkError(err)
defer resp.Body.Close()
size, err := io.Copy(file, resp.Body)
defer file.Close()
checkError(err)
fmt.Println("Just Downloaded a file %s with size %d", fileName, size)
}
func buildFileName() {
fileUrl, err := url.Parse(fullUrlFile)
checkError(err)
path := fileUrl.Path
segments := strings.Split(path, "/")
fileName = segments[len(segments)-1]
}
func httpClient() *http.Client {
client := http.Client{
CheckRedirect: func(r *http.Request, via []*http.Request) error {
r.URL.Opaque = r.URL.Path
return nil
},
}
return &client
}
func createFile() *os.File {
file, err := os.Create(fileName)
checkError(err)
return file
}
func checkError(err error) {
if err != nil {
panic(err)
}
}
Most Helpful This Week
Example of Sscan vs Sscanf vs Sscanln from FMT Package
How to check lowercase characters in a string in Golang?
Example: ReadAll, ReadDir, and ReadFile from IO Package
Find capacity of Channel, Pointer and Slice
Different ways to validate JSON string
Example of Fscan, Fscanf, and Fscanln from FMT Package