RESTful GO With MUX
By: Jordan Presnell
Tags: Rest Api, Go, Programming, Golang

Functional programming is all the rage, but with that comes a lot of new questions. There is obviously a lot to consider when thinking about functional programming, and to start; you might want to settle on a language. Of all the functional languages, none are more popular than Google's GO. As a full-stack Java developer for several years, I cannot pretend to be a GO expert. I have been playing around with the language on and off for about a year. One thing that I hear all the time about GO is… there are no frameworks! Well, that's only half true. There are new frameworks in GO. The most useful is Fiber, a web-framework that has amazing features like 0 memory allocation. For this purpose, I will show you why I think frameworks, in general, are overrated and unnecessary. We will be using a very popular and easy to set up library call MUX. MUX is an HTTP/Router library created by Gorilla and can be found here https://github.com/gorilla/mux. With MUX, you can quickly and efficiently create RESTful APIs with incredible speed and minimal code. Let’s get started!

To start, you will need to install GO. You can download GO at golang.org and follow the prompts under download. If you are on MAC good news, version 15 sets the PATH variables for you, which removes the need to do this manually with a VIM editor. Once you have GO installed, you will need to create a file in your root directory; usually, user/local/go/bin. I generally suggest creating an SRC directory and then creating my projects from there.

For this project, I will be creating a pharmacy store with a medication CRUD function. I’ve gone back and forth on this and decided not to add persistence in this tutorial, but I could be persuaded to do that later. My directory is called medication-list. The first thing you want to do is open an editor of choice; I prefer VsCode with the GO extension. Now you will want to create a file called main.go.

This is what a basic program in GO looks like!

I will assume you already understand the GO syntax and how to use imports. The next thing we need to do is install the MUX library into our project. For this project, I will be using the terminal in VsCode to install via go get github.com/gorilla/mux And now we can use it. We will want to create our variables and declare their data types, as well we will need to create their routes using MUX.

adding a simple struct and the Mux router

So, as you can see, this is just taking the slim app and doing a few more things. We imported the mux router; we created a medication struct, and we created five API endpoints for basic CRUD functions. You can obviously create as many of these endpoints as you think you will need for your app. As of right now, this app will not compile and won’t give us anything back. We first need to create the functions for each one of these methods. Before we do that, let's create some data so we can test these endpoints.

First things first, we need to serve this app!

Now we can serve our app at localhost:8000 and see the payload!

Now that we are served let's create some memory data to see a payload delivered from our endpoint.

I’m just creating three medications here in memory; you can name yours anything you want.

Now that we have an app served at localhost:8000 and some sample data, let's write an endpoint and see if it works.

Boom! You now have a REST endpoint serving your data in GO.

If we go over to localhost:8000/medications, we should see all of our medications printed on the page. Congrats, you’ve just returned a REST endpoint in 42 lines of code without a framework or 5000 spring boot files and a dreadful 90-second cold start! This whole functional thing is pretty cool, right?

Let's keep going. How about a harder endpoint? We could call a medication by ID. To do this, we will need to do first get a map in GO. There are several ways to do this, but since we have MUX, the easiest way is to use the mux.Vars() now with GO, we could use a variadic function here, but it isn’t necessary, so we will call it params := mux.Vars(). After this, it is a simple for loop on a range of ID and returning the correct ID.

Notice what's different?

So, something happened that I decided to just roll with to show off some easy GO skills. Even though we have ID as an INT in our struct, it wasn’t working, and it's because it was still looking for a string; so, I did a simple strconv.Atoi() to convert the type to INT. now if you go to localhost:8000/medications/3, you should see that medication returned on the screen.

Keep it going; we’re almost done! Let's add a medication.

So pretty standard here add a variable of medication and decode and then encode.

Two more left…

If the ID matches the ID updated in the range, then Decode the value and return the newly updated medication.

Ok, the last one, let's Remove a medication and then call this good.

This is pretty much the exact same as selecting a medication by ID, except after you select it, we append with i+1 and remove it from the map.

Welp, You made it! This whole functional thing isn’t so hard, after all. There are so many reasons to try functional programming; first and foremost, it's incredibly fast and lightweight. I’ll admit that it is different than OOP and takes a new way of thinking about code, but I hope this shows you that you can use functional programming to do just about anything you do in programming. Have fun and happy coding!

If you are interested in seeing an extended version of this with persistence built-in, please let me know.