Deploy a Go App to Railway
After using Heroku for many years, I’ve recently taken a look into Railway as an alternative hosting platform for various side projects. These projects are often written in Go (of course!) so I thought I’d write a quick guide on how to setup a new project - the good news is, it’s easy! 1) Get an Account It’s easy to setup, the link on their home page will help you.
Add Swagger Docs to a Go API
Swagger allows developers to easily create api documentation which is defined using comments in the code and then generated as a yaml config file. Using tools like Swagger has many advantages, like allowing you to generate these automatically (saving you time) and that it keeps your code and documentation as close as possible (in distance terms). The idea being that if the docs are hard to update and far away, you just won’t.
Get/Set HTTP Headers in Go Request
HTTP headers, we all need ’em 😉 Here’s how you can get and set these headers within your Go requests. These include request coming into your router handlers and requests you are sending out to other systems through net/http. This can be thought of as being the same as reading headers from a request and creating new ones. First we’ll start with reading them from the request.
Print Current Memory Usage
In this post we show how you can print the state of the current memory usage in Go. To do this, we’ve created a function PrintMemUsage() to help out, so you can call this when ever you need to know. All the info we need can be acquired through the runtime package, which allows us to read the state of the memory into the MemStats struct. It returns stats like how much memory the program is using, how much of it the OS has allocated to it and the number of garbage collections.
Change Go Version in Mod
Picking up an old Go project, we wanted to update the desired version it should run on (mainly so when we deployed to our live systems it would use this version too). To do this, we updated the version within the go.mod file - the file which keeps track of both versions and packages used by the project. We used the command below:
How to Die Dump in Go - dd()
Having come from the PHP community, we often have a handy debug function at our disposal dd() (part of Laravel) and var_dump() (native). This may not be perfect (compared with a full on debugging suite) but it is a quick and easy debugging method. This post gives an idea on how you can do the same, but in Go. We’re helped out by being able to use both variable parameters (shown with .
Get Status Code from HTTP Request
We won’t go into too much detail about HTTP status codes themselves, but in this post we will talk about how to use the status code after making a request, how to check them as a range and how to print them as text. This is often important so we can check if something was successful or failed. You can always get this data if you have a net/http/Response type (spec).
Convert an io.ReadCloser to String
Using Go, we often make HTTP calls these days using net/http, which result in a response of type io.ReadCloser… which are hard to read for the layman (like me). What we really want to the response in the form of a string which we can read. This post will talk about how to convert these ReadCloser into strings. First we’ll look at the problem, then we have two different solutions.
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.
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, gammar, 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 .