Gopher Coding Logo

Gopher Coding

Go, Docker, DevOps & Distributed Systems
  • Tutorials
  • About

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.

#download#file#io#copy#http#save#filename#stream

Convert HEIC to JPG in Go

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.

#heic#heif#encoding#images#conversion#file#types#package#apple

Find the Length of an Array

You can find the length of an array - or to be correct a slice or map - in Go by using the stdlib function len(). We’ve shown some examples below to create both a slice and a map then get the length of it. In Golang, the method is the same for both. Length of Slice We define the entire slice in one go, but you can also use append() to add to the slice.

#array#slice#map#length#append#size#sizeof#items#number

Sleeping in Golang

Sleeping in Golang

Sleeping in Go and how to pause execution and sleep for a number of seconds in Go (golang). We can use the time package from the standard library to sleep for a duration of time. You can use any duration of time, providing you use the constants provided. In our example below we sleep for two seconds, and to illustrate the point, we print out the time before and after we do this.

#sleep#golang#pause#execution#wait#goroutine#package#unix#time

Convert String to Int, Int64, Uint64

Convert String to Int, Int64, Uint64

This is one of those posts that won’t need a huge introduction. We want to convert a string into an integer. We’ll look at the different ways to do this (based on which sort of int you want). We’ll convert to int, uint and uint64 (though a int64 will be easy enough work out).

#convert#int#uint#uint64#strconv#parse#format#printf

Run Golang Tests with Github Actions

Automation is great (right?!) and it can be so easy these days to run your tests automatically on any new pull request or code change. In this post we outline how you can automatically run your Go (golang) test for your project by using Github Actions. The results will then show within Github and is configured using a yaml file. To get this running, you will need to create a workflow file (as shown below).

#github#actions#tests#automate#automation#workflow#yaml#ci

Docker Setup for Go Development

This is a short and sweet article on running a Go application using Docker. This is a generic example and many use-cases will differ and so have totally different setups. In our examples we’re also using Docker and docker-compose both of which you will need already installed and setup on your machine for this to work.

#docker#setup#development#docker-compose#alpine#env#dockerfile

Update All Go Packages in Your Project

Update All Go Packages in Your Project

If you want to update all dependencies and packages within your project then the simplest way is to use the go get command which handles it all for us. Go modules (go mod) will handle most of the versioning of these packages, so deploying and sharing the project will keep the same package versions.

#packages#modules#dependencies#all#project#versioning#get#command

Remove Old Docker Containers (Spring Cleaning)

If you just dabble with Docker, or use it regularly, you will build an arsenal of images and containers as you go along. From time-to-time we need to clear these out. This process used to be more complicated until the docker command implemented a simpler way using system prune.

#docker#prune#clear#space#image#container#system

Check If a Go Client Has Reused a Connection

For logging purposes, performance monitoring, debugging - what ever your reason, it can be useful to know if Go has reused it’s connection when making an initial request, for later use. If it’s not reusing connections, it might be running slower and less efficiently than it needs to be. The code below is used as an example of how to log if connections are being reused, as well as outputting the DNS information gathered.

#http#reuse#log#dns#httptrace#tcp#context#request
page -= 1 page += 1 page := 2
Privacy Policy
Site illustrations (of Goldie the Bird) drawn by Carina Roberts.
Tutorials & Site Content © 2023 Gopher Coding
Code Licenced under MIT Licence
Gopher Coding Logo