Download a File from a URL
This post shows how you can download a file in Go from a URL. We use the std lib (standard library) http.Get() [docs] and io.Copy() [docs] functions to help us with this. This function should be efficient as it will stream the data into the file, as opposed to downloading it all into memory, then to file. The file will be saved in the same directory as your program. We also show an alternative below if you want to take the filename from the URL.
Convert HEIC to JPG in Go
The “High Efficiency Image File Format” or HEIF is an image format often used by Apple devices. Although called HEIF, the file types are often heic (presumably the ‘c’ stands for container?) They use a similar encoding method of video formats and are deemed better quality and lower file size than standard jpeg files. In our example, we’re trying to convert these heic files back into jpg files though - so we can use them elsewhere, display them or what ever we choose.
Using a .env File & Environment Variables
Environment variables are used throughout the coding ecosystem as a way of keeping secrets out of the code. They’re also useful as a way of keeping the code the same between environments, e.g. live, uat and test servers but functionality might work differently.