The Myth about Golang Frameworks and External Libraries

Venkatesh Thallam
Oct 19, 2018 · 4 min read

I have been writing Go in production for some time now and really enjoy how fast and reliable it is. Smaller build sizes, the concurrency provided by go-routines and the fact that you could run the builds directly on the machine make Golang really sweet.

The standard packages are so performant that you could create a production ready microservice without the need for any external libraries or frameworks. But that doesn’t mean that there are no frameworks in Go which provide more flexibility or faster, they are simply not preferred.

If you ask a Go developer about what web frameworks or libraries you could use, the typical answer is stick to the standard libraries. Ironically, the top google search result for “golang frameworks” is about why you should not use them.

The top comment to a question about Go frameworks on HN

I did some research on alternatives to standard libraries to see how they perform and benchmarked them. I categorized them into what constitutes as essential components of a microservice.

All the benchmarks are performed on the below configuration. While the standard benchmark time is just 1s , I ran all the tests for 10s to have consistent cycles for each test.

Processor  - 2.7 GHz Intel Core i7RAM - 16GB - 16 GB 2133 MHz LPDDR3OS - MacOS High Sierrago version  - go1.11.1Bench Time 10s instead of standard 1sgo test -bench=. -benchtime=10s

Routing

/students/:studentID/grades/:gradeID

Any REST service which has nested resources like above would already have to use an external routing library for parsing them. Gin, Echo, Beego, Gorilla Mux, Goji are some of the popular ones (according to their Github following).
Some of these are full blown middleware frameworks which also does routing and some just are routing libraries.

I ran benchmarks against these libraries for a single named parameter and below are the results. Gin has the fastest router, followed by close second Echo.

JSON Serialization & Deserialization

Go has a really good encoding package which supports multiple formats like json, XML, csv, but a quick look at the alternatives show you tons of libraries. I benchmarked Jsoniter, EasyJson against the standard encoding/json package and below are the results.

Below is the result for Encoding and as the results indicate the performance difference is not significant.

But for decoding JSON though, jsoniter performs 5X faster than the standard encoding package.

Now if you have your request decoded, next step could be applying your business logic and may be do some database operations.

ORM or not?

But Go again has a really good database standard library which makes connecting to a relational database really easier and it’s pretty fast too. But, one pain point is for Querying. When you query some rows, you’d have to manually map each field in the row and then assign them to a struct. This works, but soon gets messy and makes you write a lot of code. sqlx is a library which allows you to scan the whole row into your struct variable.

While sqlx reduces the typical number of lines you write to build a CRUD, you still end up writing repetitive code a lot of times. Using an ORM could help reduce it and focus on your business logic.

I benchmarked database, database + sqlx, gorm , go-pg for querying and below are the results. Surprisingly, go-pg, an ORM performed faster than the standard package or even sqlx. GORM while very famous in the ecosystem is relatively slow.

Querying 200K records from a postgres DB

In each step of your API call, there are better frameworks or external libraries which will make your responses faster and also provide some flexibility.

Caveats

Despite all this, I think something like echo + jsoniter + go-pg along with all the awesomeness of the Go standard library features would be the quickest way to build your microservice and avoid redundant code.

Please feel free to share your thoughts and experiences regarding Go libraries.

HackerNoon.com

how hackers start their afternoons.

Venkatesh Thallam

Written by

Software Engineer @ Facebook. Thoughts are my own.

HackerNoon.com

how hackers start their afternoons.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade