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:

Golang: Polymorphism

Polymorphism is a bit different in Go. It supports polymorphism only through interfaces (and not through inheritance). To help you understand better, here is an example:

However, Polymorphism doesn’t work when driveACar(lammborgini) would not work if Car was a struct and not an interface. Here is an example:

Go: Declaring and using objects

Go (or Golang) doesn’t have the concept of classes. The class equivalent in Go loos like this:

The above declaration can now be used as an object like so: