paint-brush
Advantages Of Go Language For Creating Web Serversby@mattzco
239 reads

Advantages Of Go Language For Creating Web Servers

by Zco CorporationMay 29th, 2021
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Go is a universal or general-purpose language for programming languages. It can be used for web development using WebAssembly and WebAssembly. Go is also used to run microservices on Raspberry Pi and microcontrollers. Go can be written for iOS, Android, or Cross-platform, you can use Go to establish a link between the front and back ends of an app. GoLang is still in its infancy, but it is still supported by Google and is backed by Go, as well as microservices.

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - Advantages Of Go Language For Creating Web Servers
Zco Corporation HackerNoon profile picture

Use Go in your projectLooking to help bridge the information gap about different development languages, we sat down with Michael, one of our project managers, and asked him a few questions about the Go development language or GoLang. We’re going to explain where it is used, why it is useful, and some benefits associated with Go from a development and app performance standpoint.

High-level pros of using Go

  • It’s straightforward to learn. Really any programmer who has a development background with other languages like Java, C, or C++ can learn it easily.
  • The syntax, or rules that define the language for Go, is neat and clean, making it easier to understand when something is not working right.
  • Go is also very fast and lightweight.

How much faster is Go compared to something like Java?

Go directly compiles into the native binary of the host machine. It’s not like Java that compiles into bytecode. With Java you need to have a virtual machine installed on the server to run the bytecode, making it a two-step process. 

One speed example would be when calculating the factorial of a number. For example, if you write a Go and a Java program to calculate the factorial of 10,000 numbers, the Go program will do that process in around .03 seconds, while the Java program will do it in around .14 seconds.

So, you can see the difference here, and even when the number increases, Go will do the calculations in a fraction of the time that Java takes. The larger the program, the more you can see the differences.

Prominent companies utilize Go

There are many prominent companies that use Go. Primarily what we’ve seen is that whenever a company is hosting and providing media, like Netflix, YouTube, or SoundCloud, you’ll find Go.  

A lot of on-demand services like Uber and already using it too. The same can be said about anyone looking to create applications that emulate these tech giants. For example, if someone comes to us wanting to create something like Uber, we develop the front end in a native language like Swift for iOS and Kotlin for Android.

When it comes to accessing the backend server, Go is a great option, as it’s concurrency can handle thousands of requests in parallel. We’ll talk more about concurrency later. 

Now let’s look at some other ways Go is used.

The Many Uses of Go

Web Assembly

Go can be used for web development using WebAssembly. It’s not a replacement for JavaScript because JavaScript is still the ruling platform, but WebAssembly works along with it. So, if you have some code that needs higher performance, you can write that small piece of code in web assembly and use it along with JavaScript.

Knowing this, we can now compile our Go code into a web assembly binary file. You can continue writing with Go, collect or compile it into web assembly, and then you can use that in your browser.

Gopher JS

Gopher JS is a transpiler. A transpiler is something that compiles one language into a different language. It compiles the current code written in one language into a machine code binary or a bytecode like in the case of Java. GopherJS is written in Go and converts Go code into JavaScript so you can continue working on your project. It supports every syntax and every
feature of Go. This is another way of using Go in a browser other than compiling it into web assembly.

Interfacing with other programming languages

Go interfaces with different types of programming languages and is a universal or general-purpose language. No matter if an app is written for iOS, Android, or Cross-platform, you can use Go to establish a link between the front and back ends of the application.   

Suppose you already have a system built with node JS on the backend and Swift for the front end for iOS. In this case, A small section of code can be used to achieve greater performance to handle more requests in parallel and that small piece of code can be written in Go.  

Then you can use a message queue to interact between your nodeJS portion of the code and your Go portion of the code. That’s essentially a pipeline where two processes can communicate.

IoT and Robotics

Go code can run on Raspberry Pi. To run on Raspberry Pi, there is one Go compiler called TinyGo. You can use that to compile your code and get the binary to run on Arduino microcontrollers as well. So like Go , TinyGo is also backed and supported by Google. It’s still in its infancy, but you can write code using Go and then use TinyGo to compile it into binary that can run on microcontrollers.

Microservices

Diagram from Red Hat

Another way to use Go is for Microservices, to help speed up mobile applications. This is how we at Zco primarily use the language. 

You can breakdown a large system into different tasks that we’ll refer to as A, B and C. To tackle these tasks, we write microservices in Go. 

We then have a message queue that is used to communicate between each of these microservices. The beauty of this is that it can handle requests in parallel. If task A is highly backed up and having issues, that means the users are accessing a specific feature in the mobile application that requires task A to work more. 

So what you do is scale task A by putting a load balancer in front of it. This
means you don’t have to scale up Task B or C so you are saving on
resources in terms of processing power and not getting other services bogged down. So based on what features are getting more traffic in the app you can precisely scale that microservice, and that’s the beauty of microservice architecture.  

If we are putting this into layman’s terms you can think of a grocery stores checkout line. There are 4 lanes open the weekend before thanksgiving. If the employee on lane 1 is getting slammed by shoppers with their hundreds of items, you can switch that person out for someone who maybe has some more experience. More seasoned employees. This represents the load balancer. In this example all of the lanes would have the same function or job, but it paints a picture about having several services functioning at the same time.  

How Go handles concurrency

Because anything can be explained using cats.

Concurrency means you’re running tasks in parallel. It means you are time slicing the functions, allowing the processor some time to take on function one, then jump to function two, then jump to function three. So, in the end, it looks like it’s all running in parallel. 

Go is especially useful for making concurrent programs. The difference between Java and Go, and other languages like C or C++, is when you want to write simultaneous functions.  

Go has a unique feature that is built within the language that’s called Go routines. The beauty of it is that Go routines are so lightweight you can have millions of Go routines running and doing the job on a simple medium memory server.  

In terms of speed, Go routines are lightweight compared to the threads that Java uses. It’s also worth noting that writing a code to make it concurrent in Java, C or C++ is more difficult.  

Really, what you want in a web server is the ability to handle hundreds and thousands of requests in parallel. Go is specifically built for that and is hugely scalable.

Garbage Collection

Go provides a mechanism called garbage collection. What it does is initiate a small routine that runs alongside your code and keeps checking for unused memory. While running this routine, it will keep asking until it can remove the background process. 

In C or C++, you will need to clear that memory, or heap, manually. If you don’t do it, it will waste the memory capacity, and ultimately, the program will crash because of lack of memory. 

That being said, you don’t want to run garbage collection too often because your program will become slow. Thankfully, Go has an excellent enhanced garbage collector built in.  

To give an example, it’s like you‘re in a boat that’s filling up with water. You grab a bucket to shovel out the excess. The garbage collectors basically scooping out water to make sure you stay afloat. 

With the recent improvements from Go releases, the garbage collection has continued to improve even more.

Why did Zco start using Go?

So, why did we a Zco start using Go? Well, we were looking for something that could scale as our projects grew in size. At the time, we were only focusing on .NET and NodeJS, and we needed something that developers could learn quickly.   

We primarily use Go for web server development; for working with rest APIs and any background services. For example, if someone requests for an image to be processed, the request comes in, and then we run a background service that is running Go.  

One of the early programs that needed Go was a consumer-facing application because that’s where scalability comes into play. Tons of random users numbering in the thousands logged on to use the service. That creates a strain on connection speed and performance. It was also graphically intensive and needed more microservices to make sure that the connection was smooth. In popular games, the back end should be running something that can simultaneously scale and handle thousands of requests. Go is a good fit for that.  

We also plan to use Go on the front end to build web apps that run on browsers. We can do this by leveraging WebAssembly.