We're planting a tree for every job application! Click here to learn more

REST-API with Golang and Mux.

Hugo Johnsson

13 Mar 2019

7 min read

REST-API with Golang and Mux.
  • Go

In this article we are building a complete REST-API in Golang along with Mux which is a third party router.

Introduction##

Today we are building a simple REST-API with Golang a router package called Mux. We are not going to integrate with a database as this article is meant to teach you the basics but I may write another article soon, with database integration.

Creating our project##

We are going to start by creating our project which is only going to contain one file.

mkdir rest-api && cd rest-api && touch main.go

This will create a new directory called “rest-api” and a file called “main.go” in it.

We also need to install Mux:

go get -u github.com/gorilla/mux
```## Our first lines of code##

Before we write any code related to the REST-API we need to write som mandatory code to make the program run.

package main import ( "fmt" ) func main() { fmt.Println("It works!") }

Basically to import packages you need to write the “package main” and then you import statement, you also need a main function to make the it work.

To build and run the program:

go build && ./rest-api

If it prints “It works!” without any errors you are good to go!
## Initialising the router##

We are going to remove the line that prints out “It works!” as well as the “fmt” package, instead we will add the Mux package and initialise the router.

package main import ( "github.com/gorilla/mux" ) func main() { router := mux.NewRouter() }


Now we are going to establish the endpoints of our API, the way we will set this up is to create all of our endpoints in the main function, every endpoint needs a function to handle the request and we will define those below the main function.

The syntax for creating an endpoint looks like this:

router.HandleFunc("/", ).Methods("")

The” Methods” function is required because we need to tell it what type of request to accept.

Now I will create our endpoints and your file should look something like this:

package main import ( "github.com/gorilla/mux" "net/http" ) func main() { router := mux.NewRouter() router.HandleFunc("/posts", getPosts).Methods("GET") router.HandleFunc("/posts", createPost).Methods("POST") router.HandleFunc("/posts/{id}", getPost).Methods("GET") router.HandleFunc("/posts/{id}", updatePost).Methods("PUT") router.HandleFunc("/posts/{id}", deletePost).Methods("DELETE") }

What I’ve done is to create five different routes, I expect you to know at least the basics of REST-APIS so you should understand what the different enpoints does. I also added another package (“net/http”).

We will also add this line below to actually run the server:

http.ListenAndServe(":8000", router)

## Defining our models (structs)##

A struct in Golang is very similar to a ES6 or Java class. We will use a struct to represent a post, I encourage you to change up some things to make it your own, this way you will learn more. Even changing “posts” to “users” helps much in my experience.

package main import ( "github.com/gorilla/mux" "net/http" ) type Post struct { ID string json:"id" Title string json:"title" Body string json:"body" } var posts []Post func main() { router := mux.NewRouter() router.HandleFunc("/posts", getPosts).Methods("GET") router.HandleFunc("/posts", createPost).Methods("POST") router.HandleFunc("/posts/{id}", getPost).Methods("GET") router.HandleFunc("/posts/{id}", updatePost).Methods("PUT") router.HandleFunc("/posts/{id}", deletePost).Methods("DELETE") http.ListenAndServe(":8000", router) }


You can see I defined a “Post” struct, below it I also initialised what is called a slice. A slice is similar to an array, the difference is that when you want to use arrays in Golang you need to define the length. This is why we use a slice, we also tell it that it will contain posts.

If you’re confused about the struct part, basically I’ve just defined the properties as well as the type they are, then right after I just tell it how the property will be represented in JSON.
## Route handlers##

Now we just need to define the functions that will handle the requests. You also need to import the package “encoding/json”.

import ( "github.com/gorilla/mux" "net/http" "encoding/json" )


The syntax for creating these functions looks like this:

func (w http.ResponseWriter, r *http.Request) {

}

You can read more about it [here.] (https://github.com/gorilla/mux)

**getPosts()**

func getPosts(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") json.NewEncoder(w).Encode(posts) }


Here I have defined the function that gets all posts, we’re just setting the headerContent-Type” to “application/json” . Then we use the encoding package to encode all the posts as well as returning it at the same line.

**getPost()**

func getPost(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") params := mux.Vars(r) for _, item := range posts { if item.ID == params["id"] { json.NewEncoder(w).Encode(item) break } return } json.NewEncoder(w).Encode(&Post{}) }

As we are getting a specific book we need the ID from the url, we do this with this line:

params := mux.Vars(r)


and then to extract params we do:

params[""]


Then we loop though all of our books to find the one we want, when we do we are returning it the same way we did in the previous handler.

**createPost()**

Before we continue, we need to add two packages (“math/rand” and “strconv”).

import ( "github.com/gorilla/mux" "net/http" "encoding/json" "math/rand" "strconv" )


Now we can create the handler.

func createPost(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") var post Post _ = json.NewDecoder(r.Body).Decode(post) post.ID = strconv.Itoa(rand.Intn(1000000)) posts = append(posts, post) json.NewEncoder(w).Encode(&post) }


This is the handler that creates a new post, we start by creating a new instance of the struct Post. Then we decode the data that is sent with the request and insert it into the post. Next we create a random ID with the “math/rand” package as well as convert to a string. At last we simply append the post to our posts array, this will save it to memory, and right after we return the new post.

**updatePost()**

func updatePost(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") params := mux.Vars(r) for index, item := range posts { if item.ID == params["id"] { posts = append(posts[:index], posts[index+1:]...) var post Post _ = json.NewDecoder(r.Body).Decode(post) post.ID = params["id"] posts = append(posts, post) json.NewEncoder(w).Encode(&post) return } } json.NewEncoder(w).Encode(posts) }


For the update handler, we loop through our posts array to find the post to update. When it matches we remove that post from the array and create a new post with the same ID (using params[“id”]) with the new values from the request body.

**deletePost()**

func deletePost(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") params := mux.Vars(r) for _, item := range posts { if item.ID == params["id"] { posts= append(posts[:index], posts[index+1]...) break } } json.NewEncoder(w).Encode(books) }


Here we are also looping through our posts array to find the post we want to delete. When we do, we delete the post with this line:

posts = append(posts[:index], posts[index+1]...)


and then we break out of our loop.

**Adding mock data**

Before we test we just need to add some mock data, this is done in the main function right after we initialise the router. Make sure it looks like this:

func main() { router := mux.NewRouter() posts = append(posts, Post{ID: "1", Title: "My first post", Body: "This is the content of my first post"}) posts = append(posts, Post{ID: "2", Title: "My second post", Body: "This is the content of my second post"}) router.HandleFunc("/posts", getPosts).Methods("GET") router.HandleFunc("/posts", createPost).Methods("POST") router.HandleFunc("/posts/{id}", getPost).Methods("GET") router.HandleFunc("/posts/{id}", updatePost).Methods("PUT") router.HandleFunc("/posts/{id}", deletePost).Methods("DELETE") http.ListenAndServe(":8000", router) }

Your file should now look something like this:

package main import ( "github.com/gorilla/mux" "net/http" "encoding/json" "math/rand" "strconv" ) type Post struct { ID string json:"id" Title string json:"title" Body string json:"body" } var posts []Post func getPosts(w http.ResponseWriter, r http.Request) { w.Header().Set("Content-Type", "application/json") json.NewEncoder(w).Encode(posts) } func createPost(w http.ResponseWriter, r http.Request) { w.Header().Set("Content-Type", "application/json") var post Post = json.NewDecoder(r.Body).Decode(&post) post.ID = strconv.Itoa(rand.Intn(1000000)) posts = append(posts, post) json.NewEncoder(w).Encode(&post) } func getPost(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") params := mux.Vars(r) for , item := range posts { if item.ID == params["id"] { json.NewEncoder(w).Encode(item) return } } json.NewEncoder(w).Encode(&Post{}) } func updatePost(w http.ResponseWriter, r http.Request) { w.Header().Set("Content-Type", "application/json") params := mux.Vars(r) for index, item := range posts { if item.ID == params["id"] { posts = append(posts[:index], posts[index+1:]...) var post Post _ = json.NewDecoder(r.Body).Decode(&post) post.ID = params["id"] posts = append(posts, post) json.NewEncoder(w).Encode(&post) return } } json.NewEncoder(w).Encode(posts) } func deletePost(w http.ResponseWriter, r http.Request) { w.Header().Set("Content-Type", "application/json") params := mux.Vars(r) for index, item := range posts { if item.ID == params["id"] { posts = append(posts[:index], posts[index+1:]...) break } } json.NewEncoder(w).Encode(posts) } func main() { router := mux.NewRouter() posts = append(posts, Post{ID: "1", Title: "My first post", Body: "This is the content of my first post"}) router.HandleFunc("/posts", getPosts).Methods("GET") router.HandleFunc("/posts", createPost).Methods("POST") router.HandleFunc("/posts/{id}", getPost).Methods("GET") router.HandleFunc("/posts/{id}", updatePost).Methods("PUT") router.HandleFunc("/posts/{id}", deletePost).Methods("DELETE") http.ListenAndServe(":8000", router) }


I’m going to use Postman to test the API but curl is also a good option, but I like Postman’s graphical interface. You can get Postman [here.](https://www.getpostman.com)

Make sure you build and restart/run the server:

go build && ./rest-api

**Getting all posts**
![1_S1OKMhH-5vmxrCLuR8StEg.png](https://functionalworks\-backend\-\-prod.s3.amazonaws.com/logos/0e071acfac17e2a66ff0648447360d53)

**Getting a single post**

![1_A_1vQucUuLr3PkiAHdjbRA 2.png](https://functionalworks\-backend\-\-prod.s3.amazonaws.com/logos/396649e843ad72d8f81b56aa1142f2b6)

**Creating a new post**


![Creating a new post.png](https://functionalworks\-backend\-\-prod.s3.amazonaws.com/logos/7c7b5fc713c8b1e10a95f4864e059243)

**Updating a post**


![updating a post.png](https://functionalworks\-backend\-\-prod.s3.amazonaws.com/logos/00e3ce64435ff249e1cfafd0926d7dcf)

**Deleting a post**


![Deleting a post.png](https://functionalworks\-backend\-\-prod.s3.amazonaws.com/logos/4c79f089f0ee9565cedc8a41bbb6b96c)
## Thank you so much for reading!##

I hope you found this article helpful, I really hope I helped at least one person get familiar with the basics of building REST-APIs with Golang and Mux. If you didn’t follow along and built it with me I encourage you to do so, you learn so much faster when you actually type the code:)
Did you like this article?

Hugo Johnsson

See other articles by Hugo

Related jobs

See all

Title

The company

  • Remote

Title

The company

  • Remote

Title

The company

  • Remote

Title

The company

  • Remote

Related articles

JavaScript Functional Style Made Simple

JavaScript Functional Style Made Simple

Daniel Boros

12 Sep 2021

JavaScript Functional Style Made Simple

JavaScript Functional Style Made Simple

Daniel Boros

12 Sep 2021

WorksHub

CareersCompaniesSitemapFunctional WorksBlockchain WorksJavaScript WorksAI WorksGolang WorksJava WorksPython WorksRemote Works

Articles

email iconhello@works-hub.comUK flag

Ground Floor, Verse Building, 18 Brunswick Place, London, N1 6DZ

US flag

108 E 16th Street, New York, NY 10003

Subscribe to our newsletter

Join over 111,000 others and get access to exclusive content, job opportunities and more!

© 2023 WorksHub

Privacy PolicyDeveloped by WorksHub