Welcome To Golang By Example

Menu
  • Home
  • Blog
Menu

Download an image or file from a URL in Go (Golang)

Posted on February 21, 2023September 26, 2023 by admin

Table of Contents

  • Overview
  • Code

Overview

Below are the steps to download an image or a file from a URL

  • use http.Get(URL) function to get the bytes of the file from the URL.
  • Create an empty file using os.Create() function
  • Use io.Copy() function to copy the downloaded bytes to the file created in step 2

Code

package main
import (
"errors"
"fmt"
"io"
"log"
"net/http"
"os"
)
func main() {
fileName := "sample.pdf"
URL := "http://www.africau.edu/images/default/sample.pdf"
err := downloadFile(URL, fileName)
if err != nil {
log.Fatal(err)
}
fmt.Printf("File %s downlaod in current working directory", fileName)
}
func downloadFile(URL, fileName string) error {
//Get the response bytes from the url
response, err := http.Get(URL)
if err != nil {
return err
}
defer response.Body.Close()
if response.StatusCode != 200 {
return errors.New("Received non 200 response code")
}
//Create a empty file
file, err := os.Create(fileName)
if err != nil {
return err
}
defer file.Close()
//Write the bytes to the fiel
_, err = io.Copy(file, response.Body)
if err != nil {
return err
}
return nil
}

Output:

File sample.pdf downlaod in current working directory
  • golang
  • pdf
  • Popular Articles

    Golang Comprehensive Tutorial Series

    All Design Patterns in Go (Golang)

    Slice in golang

    Variables in Go (Golang) – Complete Guide

    OOP: Inheritance in GOLANG complete guide

    Using Context Package in GO (Golang) – Complete Guide

    All data types in Golang with examples

    Understanding time and date in Go (Golang) – Complete Guide

    ©2023 Welcome To Golang By Example | Design: Web XP