So, I wanted to learn Go for a while now and finally found the time to do so. I will attempt to build a simple REST API with Go. Our API will have 2 endpoints.
GET /quote # Will return a random inspirational quote
POST /quote # Will add a new inspirational quote to the database.
Users can submit inspirational quotes to the API when they are feeling inspired. And if users are in need of inspiration they can get a random inspirational quote.
Yes, this API will definitely make the world a better place.
Setting up the project
The first rabbit hole I’m diving into is how to set up a Go project. I found out that most Go programmers have a single workspace in which they store all their projects.
The first step is to set up my workspace ~/.go
folder and export the GOPATH
variable.
mkdir ~/.go
export $GOPATH="${HOME}/.go"
Each workspace should have 2 folders.
~/.go/src
is where the source code is located.~/.go/bin
is where the compiled programs are located.
Next, I’m creating the folder for the Quote API project in the ~/.go/src/quote-api
folder.