Skip to content

download file and store it in CWD, by just passing the file name & URL

License

Notifications You must be signed in to change notification settings

codeArtisanry/goDownloadFile

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

goDownloadFile

download file and store it in CWD, by just passing the file name & URL

import package in golang file

import (github.com/codeArtisanry/goDownloadFile)
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
}

Use directly this Function by

go get github.com/codeArtisanry/goDownloadFile

About

download file and store it in CWD, by just passing the file name & URL

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages