0x00 - Microservices in Go (Part 1)
0. Setting up your environment⌗
Installing Go⌗
You can find the instructions for installing Go on your OS here.
Once you have installed Go you can run the following command to verify that the installation succeeded:
Editor/IDE⌗
For programming in Go, I use Visual Studio Code as my IDE with the Go extension and I highly recommend using it.
1. Boilerplate⌗
Let’s create a new directory, and set up the boilerplate code.
This is the content of my go.mod
file:
2. A Simple HTTP server⌗
Let’s create a very simple HTTP server in a file main.go
at the same level as the go.mod
file. Here are the contents of the file:
We’re using the net/http
package from the Go standard library to build our simple HTTP server. The function handler
is associated with the /
URL path and is invoked when an HTTP request is made to http://localhost:8080/
. The function logs the message
string to the console and also returns it as the response body through the call to rw.Write()
.
Building and running the server⌗
To start the server, let’s build and run the program:
Testing⌗
Once the server is running, you can use curl
to make a request:
And there will be a log line:
To look at other details such as status code and headers, let’s run curl
with the verbose flag -v
:
Conclusion⌗
In this part of the tutorial, we’ve set up our environment and built a simple HTTP server that returns a response. Astute readers would have observed that our handler returns the same response regardless of the HTTP method (GET, PUT, POST, DELETE, etc.) of the request. We’re going to improve our server in future tutorials so that we have different logic for different methods.
You can find the code for this part of the tutorial here.