Gopher Coding Logo

Gopher Coding

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

Example Netlify Function in Go

Netlify, the hosting and web platform, allows you to create "functions" along with their CDN. These functions are hosted on AWS’ Lambda and can be accessible via a URL. So you can create static sites, with extra ability and dynamism (like we use on this site). We wanted to share a post giving an example how to write one of these functions in Go. The aim of the code (below) is to return the version of golang.

#netlify#function#aws#lambda#cli#serverless#gateway#static

Remove All Non-Alphanumeric Characters

We often need to remove symbols and special characters from the strings we’re using (especially with currency!). This post shows how you can keep the letters and numbers, but remove any punctuation, symbols, grammar, etc. For example, if a user types in “$1,000” you can turn it into “1000”. We use the regexp package to do this, first building a regex with .Compile() then running the string through that regex with .ReplaceAllString(). Finally with display and compare both strings.

#remove#regexp#alphanum#character#symbol#replace#compile

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 standard library function len(). We use the term array loosely here, as a general variable holding multiple things. Maps tend to have defined keys, whereas slices don’t (more info on the difference here). We have shown both as examples below, how to get the length of a slice and how to get the length of a map. In Golang, the method is the same for both.

#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). Which will essentially do the following:

#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
page -= 1 page += 1 page := 4
Privacy Policy
Site illustrations (of Goldie the Bird) drawn by Carina Roberts.
Tutorials & Site Content © 2024 Gopher Coding
Code Licenced under MIT Licence
Gopher Coding Logo