Golang: Restart web server on file change

A great feature of scripting languages like PHP, Python and Ruby is that you don’t need to re-compile the app or restart a web server every time you change something. With Go, you need to restart the web server for your changes to take affect. This can be a pretty daunting task. We can, however, […]

Golang: Get the function caller’s name

Problem Consider this code:

In the function foo, we want to get the name of the function (and preferably file name and number ) that called it. Solution We can get this information by traversing the stack trace (which Go’s “runtime” package has handy functions for:

Golang: Connect to Postgres and selecting data from a table

You will need to get the Postgres driver first. In the terminal type:

Connecting to Postgres

Selecting data from a table After connecting to the database, you can do the following:

Golang: Testing HTTP requests

Unit testing HTTP calls to external services is pretty easy in Go. Let’s say we have some code that makes HTTP requests like so:

We can test if the request is made and we get the response we want by mocking the external service. Below is the code:

Golang: Make HTTP requests

A simple GET request can be made like this:

POST request:

If more control is needed, like specifying headers, cookies, etc: