Every computing language has its history, strong points and a framework around which it works. Each language has its era, a peak period of efficiency, utility, and popularity.

Among the programming languages that are in prolific use, we talk about two prime languages here – Ruby and Golang.

Ruby is a dynamic open source programming language focused on simplicity and productivity. It also has elegant syntax that is easy to read and to write.

The main purpose of Ruby is to easily create simple and intuitive programs within a short period, while the resulting programs do not necessarily have to work very fast. Not long ago Ruby was the first choice language for many programs for reasons described above.

But, as time passes, anything that needs to get improved, does. In 2007, Google was looking to create a simple and powerful system programming language capable of creating high-performance programs that would work with distributed systems and multiple-core processors.

Below is a breakdown of the main reasons why Go has enjoyed increasing popularity among developers.

1. Go is Definitely Faster

Go is a compiled language and so it’s very fast (3x-6x times faster than Ruby). Ruby is getting better, but it has never been fast compared to other popular languages. Here’s a breakdown that includes both Go and Ruby (more).

Reverse-complement

SourcesecsKBCPU
Ruby5.24686,7649.66
Go0.5388,3680.94

Pi digits

SourcesecsKBCPU
Ruby11.18163,16811.16
Go2.8511,1402.88

K-nucleotide

SourcesecsKBCPU
Ruby80.44381,61680.37
Go24.38267,07275.31

2. Concurrency and Parallelism

One of the strongest sides of Go is a built-in concurrency based on Tony Hoare’s CSP. Any function or method in Go can be created as a goroutine.

We can consider that the main function is executing as a goroutine, however, the Go runtime does not start that goroutine. Goroutines are considered to be lightweight because they use little memory and resources, plus their initial stack size is small.

Prior to version 1.2 the stack size started at 4K and now as of version 1.4 it starts at 8K. The stack has the ability to grow as needed. By default, the Go runtime allocates a single logical processor to execute all the goroutines that are created for a program.

Even with this single logical processor and operating system thread, hundreds of thousands of goroutines can be scheduled to run concurrently with amazing efficiency and performance.

3. Cross-Compilation

Cross-compilation produces static linked binaries. It allows for preparing a binary on one machine and running it on any other machine. There is no need to set any dependencies.

4. Strong Typing

Go is a strong typed language that makes it easier to write in and allows to avoid many bugs. Much (but not all) of safety is enforced by strong typing — this term, too, is not well defined, so let’s explain what we mean by it here.

First, the type of every variable and every expression is a syntactic property—known by looking at the program, without resorting to executing it.

Second, a variable may be used only in ways that respect its type — for example, it is impossible to perform double operations on an integer value (the integer value must first be cast to double format) or to use a Boolean value as an integer. Thus, it is syntactically impossible to have an operation disregard the types of its operands, to interpret a value as something that it is not.

5. Golang is Simple

Go has a single way to do things, helping keep code clean.

Here’s a full working example of a simple web server:

package main

import (
    "fmt"
    "net/http"
)

func handler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:])
}

func main() {
    http.HandleFunc("/", handler)
    http.ListenAndServe(":8080", nil)
}

6. Powerful Reflection Tools

Go has powerful reflection tools compared to Ruby, which makes it easier to develop generic tools or basic mechanisms. In Go reflection allows inspection of structures, interfaces, fields and methods at runtime without knowing the names of the interfaces, fields, methods at the time of compilation. It also allows for instantiation of new objects and invocation of methods.

7. Go’s Error Handling is Elegant

Interpreted languages, like Ruby, are more concise because it is not required to handle exceptions at all. Swift and Java won’t compile until you force the caller to handle the exception. Go won’t compile unless the caller handles or ignores the error value.

With Ruby, you just have to know that a method might raise an exception. Rails uses a bang in the method name (e.g. model.save!) to show that some sort of exception may be raised. But otherwise, good luck trying to find when, where, or what. Therefore, it’s more common in interpreted languages to see exceptions at runtime. Go (along with Swift and Java) try to tell you about all error cases up front to limit unexpected behavior at runtime.

Go can be very powerful while relatively simple. Certainly, you can find faster languages and easier languages but the equilibrium Go gives between optimization and complexity is ideal for a lot of use cases I believe.

Get the conversation started!

Discover how Velvetech can help your project take off today.
Please leave this field empty.