Golang and a Pluggable, Configurable CRUD API

Lessons from the continual creation of a CRUD Framework — FRUD

Ryan Hancock
5 min readMar 31, 2018

You can find the project I will talking about here.

I’ve been working with Node.JS for the past couple years as it was the language of choice for many developers around me. I’m not particularly attached to any language as long as the said language allows me to work with data and web effectively. I’ve been looking around at random projects on Github and found more often than not many new projects were adopting Golang. After a couple weeks of seeing it always pop up I decided to try it out to see how I liked it. I started work on my open source project FRUD (I’m not particularly attached to the name). I wanted to create a framework that had the following services:

  1. Give the user the ability to work with just JSON (example) and define their data model objects and the database these objects will be stored in.
  2. Allow the user to give the server the code themselves for their endpoints, as to handle any edge cases the framework doesn’t handle (example).

I wanted to give user the ability to quickly create backend API’s for themselves. If it be for some proof of concept, or maybe a simpler backend service.

First Impressions

I’ll tell you right away. I really really like Golang, it feels like a language that gives me all the tools I need within its standard library to build responsive web based tools and API’s, but I also feel like I don’t lose precision of my language. One thing I really missed when working with Javascript is the ability to easily work with memory, and actually feel like I’m controlling how my memory is being used. Golang gives that control back. Golang also forces definitions. Now let me tell you, I’m a math guy, I love definitions. I like things being clearly defined and laid out as it increases readability and maintainability ten fold. You have some JSON object you need to bring into a usable format? Define a struct and Unmarshal the JSON into the struct. You have to define you’re data in some capacity to use it effectively. With FRUD this presents some challenges as I don’t know what my data objects are going to be when I use them. I can however define how the data is laid out which allows me to figure out what the user has defined. Golang ventures away from a world filled with object oriented programming and creates a more functional language with inspirations from object oriented.

Golang is yissssssssssss.

Another wonderful and annoying thing is that the compiler will fail if you have a variable that you defined but never use. I mean this is a very small thing, but I think it shows everything that Golang tries to accomplish. It is a language that forces good programming (to an extent). It does this by being very opinionated, which I feel is all around a nice thing. Another fantastic quality is that tools like godoc are built into the language, testing is built into the language. Ability to build and test HTTP dependent applications is just… there. Its a modern language that feels as precise as C. Which makes sense considering one of the designers of C helped design Golang.

Talking FRUD

My first initial challenge was solving the plugin method of allowing users to provide their own code. I ventured into the reflect library and types library, but still couldn’t find a suitable solution that allowed me to call functions within other sources of code, until I came across a suitably named — plugin — library. This finally made the solution to my problem rather trivial. The next step was creating a maintainable way of dealing with configuration objects. Now my first whack at it was… bad. I got too excited when I was coding with this language that I think I did a rather poor job. I redid it so that the configuration object only validates its direct fields and then asks its children to validate. This allowed for MUCH cleaner code.

I then needed to turn to databases. I started with Neo4J, which I felt would be the hardest as it is a non traditional database, using nodes and relationships (graphs) to store data rather then tables or documents. I juggled around with bits of code but decided to almost completely redo it as I wanted as little static strings in my code as possible. I wanted to create my own system of generating Cypher queries with a database request object. I ended up going with a style that I thought would be fun to program and try, I don’t recall if it has a specific name, but in some libraries for testing you can take a value and test it by writing Value(VALUE_HERE).Is().Equal().To(ANOTHER_VALUE). I thought this would be a fun style to try and write, the final outcome can be found here.

Picture unrelated. Its just funny.

Final Thoughts

The style of Go feels powerful, adapted to a web based world, concise, fast, and readable. Some of the folks that dislike it come from a strong background of object oriented languages, so they miss things like generics, and inheritance etc. But for me as someone who has never fully embraced that style (#FunctionalForLife) it was a rather easy transition to make. One big component of Go I have yet to completely try is channels and go routines, so I will not post my opinions about it just yet. I’m fully enjoying FRUD and working on it and see potential for people to use it. I will gladly accept any pull requests or suggestions as I still very much am a Go noob. But I will definitely spend more time perfecting my ability to program in Go and hopefully use it for all future side projects. I highly suggest anyone who hasn’t tried it to give it a … go.

I apologize.

--

--

Ryan Hancock

My goal is to share my life, experiences, knowledge, and passion with anyone who cares to read. Currently a PhD student at the University of Waterloo.