Create a simple REST API using GOLANG

A random YouTubers' voice, "this is not a Golang crash course, make sure you have prerequisite knowledge about the language."
What is a REST API?
A REST API (also known as RESTful API) is an application programming interface (API or web API) that conforms to the constraints of REST architectural style and allows for interaction with RESTful web services. REST stands for representational state transfer and was created by computer scientist Roy Fielding. ( RedHat , 2020)
Creating your Project
- Create a new folder.
CDinto the folder and create amain.gofile.- Create a
go.modfile usinggo mod init {something.something.com} - Install gorilla mux using the
go get github.com/gorilla/muxcommand.

Setting up your server

What is going on under the hood?
First, we declared a variable called router and assigned mux.NewRouter() to it, remember we installed gorilla mux at the beginning of this tutorial. What the NewRouter() does is that it creates a new router instance.
In the second line, you can see something like a router.HandleFunc(), which takes in a path and the controller name. What the HandleFunc() does is that it registers a new route with the matcher for the URL path.
We then go on to declare the port number (address) and also an Handler using the http.ListenAndServe()
Creating your controller

We created a function called getBooks which will return all books in the the REST API. Two parameters were parsed into the function, which is w http.ResponseWriter, r *http.Request. A response writer is used to construct a response. A request represents a HTTP request received by a server or to be sent by the client.
w.Header().Set("Access-Control-Allow-Origin", "*") - CORS (you'll get a tutorial update on this soon).
w.Header().Set("Content-Type", "application/json") - This is us setting up the content type.
json.NewEncoder(w).Encode(books) - This is the OG, this is what display all the rubbish we've been doing.
So, my comrade, I hope you now understand how to create a simple REST API in golang.
You can check out the Code on Github Here: Github