Golang as a Backend!?

Golang as a Backend!?

Developed by Google in 2009, Golang is an open-source language that has been steadily gaining traction. Is Golang a good choice for your next backend?

Where speed and scalability are needed, Go is often opted. Particularly for high-performing concurrent services on the server side. Folks are also using it to build the server side of their mobile apps.

What is Golang?

Go is a statically typed, compiled programming language that was made open-source back in 2012 by Google. It quickly rose in popularity and became many developers' first choice due to its simplicity, readability, efficiency, and concurrent nature. Golang provides a robust dependency management system and it also comes with a Garbage collector. Golang or Go makes a perfect choice in many scenarios. The project has grown very fast, and count now more than 1877 people contributing to it.

What can you build with Golang?

Golang is extremely versatile when it comes to building projects. From high and low level programming to building products for infrastructure, go is everywhere. You can use go to create completely new frameworks or use the existing web frameworks to create different websites, which can be an E-commerce application or a social media website, a lot can be done with golang. We can write Cloud-Native softwares, golang was meant to be written on the cloud. Go has also found its place in building SAAS products that could be microservices-driven or server-driven SAAS products. Lastly, you can go serverless as well.

Benefits of using Golang as a backend

Golang offers multiple perks when writing the backend of your dream project. With its simplicity and high performant features, golang is also easy to learn and comes with built-in concurrency.

Cross-platform support

Go makes an ideal choice for backend web development, particularly for high-performing concurrent services on the server side. You are not limited to that, golang can be used to build the server side of your mobile app too. Take an example, the GUI part of the Android app can be done in Java, while the app backend can use Golang at its best. Golang also has demand in data science because of the reasons including easy deployability, and built-in error checks.

Concurrency

When modern applications are built for real-time collaborations, they increasingly rely on microservices. Golang comes with built-in features to support concurrent web requests which are highly desirable for any software these days. Concurrency is led efficiently with keeping the execution straightforward. It enables connection with millions of users from a single instance and does not block any web request while interacting with a number of services.

Scalability

You have seen Golang and its ability to support concurrency. Golang presents Goroutines, which are basically functions that can run simultaneously and independently. Going further, Goroutines take up only 2 KB of memory, making them scalable when the need for running multiple concurrent processes arises. Taking examples from other programming languages like Java's threads, which are blocky, Goroutines are non-blocky. Unlike Java's threads being heavyweight, which gobble up a lot of memory. You can run millions of goroutines without crashing the system. Remember, having leaner and meaner software gives you an edge over your competitors.

Simplicity

Golang has packages that give you isolated environments when you are working on loosely connected services. The language becomes an ideal choice if you have a large codebase with different teams working on different segments of the code. Modification and maintenance become a breeze compared to languages like python and Java.

Fast and High Performant

Golang is fast, it is compiled directly into formats understandable by the processors, and does not need any virtual machine or such, to convert the code into machine-readable formats. Golang was initially built to solve the build time issues at Google. Go code executes as fast as Javascript on a webpage, Golang has been proven to be generally faster than Java and python. The Garbage collection in golang helps to avoid loading delays, eventually boosting the speed of the application.

Easy to get started

With a simpler syntax, based on the C programming language, Golang is easy to learn. While the keywords and syntax may slightly defer, Go has the same procedural approach that programmers will familiarize in no time.

Using Golang over NodeJs ?

No language or framework is perfect in any way. If there's one thing where Nodejs excels, then there are places where Golang leaves everyone ahead. We compare these two languages based on four factors. Performance, community, packages, concurrency and resource utilization.

Performance

When it comes to performance, Golang is way ahead of NodeJs. There is no contest on that matter. There is a lot of content out there supporting that Golang will take over NodeJs in no time. Here's an article by the Uber Engineering team, about how they built their highest query per second service with go. Many companies are migrating from NodeJs to Golang.

Community

No doubt the NodeJs community is much much bigger than Golang's community, thanks to the Javascript folks. But Golang is gaining traction among a lot of young developers and several companies are also switching to Golang. We'll talk about this in a moment.

Package managers

In terms of packages available, NodeJs is winning for a long time due to its package manager npm which has been there for quite a while. But golang does not lag in so, it has recently got the go mod feature, golang is getting there, and a lot of packages are coming up. Basically, there's everything you need, but again, npm is the legacy package manager.

Concurrency and Resource Utilization

Golang again shines in here, it comes with in-built concurrency support, with a lot of parallel processes and several multi-tethering threads, making it extremely performant and resource utilization is also very low in golang because you can completely take benefits from the different multiple cores that your processor has. This is why it is a win for Golang here.

So that's about it. Golang doesn't seem to be missing out on anything, and whatever you feel is missing, will be available in a few years.

Top Frameworks

gin

The gin framework comes on top of the list in terms of popularity due to its performance records. It is mostly used for building REST APIs. It also has rich documentation provided on Github, making it ideal for beginners.

beego

The beego framework is used for rapid development of REST APIs, web applications, and backend services in Golang. It is often viewed as similar to the Django web framework in python. The beego framework comes with a tool called Bee tool that helps with finding code changes in the project. It is a full-fledged Model-View-Control (MVC) framework with its own libraries.

echo

Another high-performance, blazingly fast framework in Golang. Echo comes with a highly optimized HTTP router with zero dynamic memory allocation that smartly prioritizes routers.

kit

The kit framework is a programmer's toolkit for building robust, reliable, and maintainable microservices in Golang. The framework provides Remote Procedure Call safety, system observability, and infrastructure integration.

fasthttp

The fasthttp framework provides a fast HTTP server and client API which comes as an alternative to net/http due to its limits on optimization opportunities.

Companies using Golang

Golang is already in production and people are loving it. More than 75% of the CNCF projects are written in Golang. Some renowned ones include Docker, Kubernetes, Terraform, Prometheus, etc. And this is what excites me the most to learn more about this language. Apart from that, companies like Google, Twitter, Netflix, twitch, Meta, Microsoft, Uber, and many others have been using Golang for quite some time.

Usecases

Here are some more use cases of Golang which are really remarkable:

  • Cloud and network services
  • Command-line Interfaces
  • Development Operations
  • Site Reliability Engineering

Case Studies

Here are a few example case studies taken from the official website.

  • Check out what allegro has to say:

Finally we sped our application from more than 2.5 seconds to less than 250 milliseconds for the longest request.

You can check out more at go.dev/solutions/#case-studies

Conclusion

Alright, so far so good, let's quickly recall whatever we have discussed. We talked about what Golang is and what can you build with it, then we saw what are the benefits of using Golang on the backend, later on, we did a quick comparison between go and NodeJs and listed out the top 5 frameworks in golang. Golang doesn't seem to be just another trend in the market. Companies are loving it, and I think Go is a really great choice if you want to build a highly scalable product. The Golang community is undoubtedly welcoming and it's always fun to learn a new language.

Hey, thanks for stopping by, I am Shafaq and this was my very first blog on Hashnode. You can follow me on Twitter for more stuff related to Golang.

Feedbacks are always appreciated :)