Create REST API with GoLang and MongoDB

Ahmet Faruk
5 min readJun 30, 2019

--

Hi everyone,

Nowadays, I am researching go lang and I want to apply my personal project. In this article, I will share my experience about go lang and Also we will develop restApi which included CRUD operations.

Go is an open-source language. It is very easy compiled to machine language. One of the most important features is Go has concurrency patterns. Its syntax is a bit confusing. If you start to develop something with Go, you will see often & or * characters. Also, when you want to add a method in your class, this process could be stranger than you think. If you wonder about this topic, you can visit for detail here.

Firstly, we need to install Go. You can install here. Then we need IDE. I prefer to Visual Studio Code. If you haven’t, you can download here. Then, We will create a directory to work in. Go has a rule for this case. They say that you should have one workspace whose name is ”GO” and you should have two base package in this workspace. You can check here for more detail. Also, the directory structure looks like the below image.

if you use windows, workspace could be in C:/Users/username/

If you have done these steps. We can create our application. We have to create main.go files. Go wait for us to create this file. If we haven’t it and we try to run our application, we will get this error; “go run: cannot run non-main package”. Now, we are creating main.go file in the root. If you are curious about some topic which like variable type, how to import class, conditionals, loops, etc. You can check this sample code.

Now, we are starting to develop our API application. We need some libraries which like mux, mongo, net/http, etc. Some of them are in Go core libraries, another of them are 3rd party library. We have to install them.

You can run this command for Mux. > go get -u github.com/gorilla/mux Mux manages our routing process. It is similar to Express.js in Node. And you can run this command for MongoDB> go get go.mongodb.org/mongo-driver

Note: The output of this may look like a warning stating something like package go.mongodb.org/mongo-driver: no Go files in (...). This is expected output from go get. don’t worry, the correct packages should be retrieved at build time.

By the way, you must have MongoDB. If you want, you can install that in your local machine or you can use MongoDB Atlas which is MongoDB's cloud product. I prefer MongoDB Atlas because I don’t want to work its configuration in my local machine. You can create a cluster in the cloud here. Then, you can add our dependencies like below

Go has got unusual features. If you add some library and you never use that. Go will automatically remove them. Also, you define a variable and you didn’t use it. You will get an error involved with that. If you don’t see the packages that you added these, don’t worry, now you know why :)

As far as here, we installed Mux and mongo-driver package and added our dependencies. Now, we are creating our structs. Therefore, we are creating models directory then we will create models.go file in this directory. By the way, in Go, There is not the class. We use struct instead of class. You follow the code below

We defined json and bson name to them when our structs serialize. omitempty means that If we didn’t assign any value our struct’s field, don’t show this field after serialize process. Also, bson is relevant to mongo-driver. It works for creating filter query. We will see as soon as.

We are preparing the routing process in the main function. You can follow the code below

I think, variable definition could be unusual in Go. If you think like this, you can check here.

Now, we are creating a helper class for connection database stuff. That’s why we will add the directory that name is helper and we will create connection.go file in this directory. You can follow the code below.

We will create our methods that pass in HandleFunc. That’s why we are expanding our main.go class. Firstly, We are creating getBooks function. You follow the code below.

After we added getBooks function, we will create getBook function.

Then, in order, we will add createBook, updateBook, and deleteBook methods.

We have almost finished. Make sure about saved all files. Then, we’ll run these commands sequentially. > go build main.go and > go run ./

Everything is ready now. We are opening Postman. And we will see the results. Let’s do that.

Create Book
Update Book
Get All Books
Get Book
Delete Book

Well done. We developed the first rest API project. In the next part, I want to implement the authentication step in this project.

Ok guys, see you in the next article. If you’ll have a question you can ask me without hesitation. Also, you can access this project here

--

--