DEV Community

jefferson otoni lima
jefferson otoni lima

Posted on

Why We Love Go! ❤

Go: The fastest growing language for backend

Go was designed by Google in 2007 to improve programming productivity in an era of multicore, networked machines, and large codebases. The designers wanted to address criticisms of other languages in use at Google, but keep their useful features. The creators Rob Pike, Ken Thompson, and Robert Griesemer kept Go's syntax similar to C. In late 2008, Russ Cox joined the team and helped move the language and libraries from prototype to reality.

The Go language was released in 2009 with the purpose of making it easier to solve problems related to networked layers, scalability, performance, productivity, and most importantly, concurrency. Rob Pike himself declared that "Go was designed to address a set of engineering problems we encountered building large server software."

Go has been influenced by various programming languages and different paradigms, including Alef, APL, BCPL, C, CSP, Limbo, Modula, Newsqueak, Oberon, occam, Pascal, Smalltalk, and Crystal. It is clear that they used what was best and created something new and lean, with the minimum necessary to solve the proposed problems without losing its simplicity. I believe this is what we can call innovation. Go broke the paradigms of languages and implemented something new in a simple and very powerful way.

package mainimport "fmt"func main() {
    fmt.Println("Hello all folks!")
}
Enter fullscreen mode Exit fullscreen mode

Less is exponentially more

The Go developer team say its creation is an attempt to make programmers more productive. Improving the software development process on Google. The first goal was to create a better language to address the challenges of scalable concurrency, ie software that handles many concerns simultaneously, an example would be coordinating a thousand back-end servers sending network traffic all the time.

Keeping the Go language small allows for more important goals. Being small makes Go easier to learn, easier to understand, easier to implement, easier to reimplement, easier to debug, easier to tweak, and easier to evolve. Doing less allows for more. It's an expression used by the Go development team: “Do Less. Enable More” would be the balance between the universe of existing problems and what Go can help to solve such problems well. Go explicitly wasn't designed to solve every problem, instead they've done enough for us to create our own custom solutions with ease, while making it very clear that Go can't do everything.

Uproar in the Go community

Several features of modern languages were left out, contradicting and going in the opposite direction of other current languages, some features such as: the use of immutability declarations, pattern matching data types, generics, exception handling, inheritance, generic programming, assertion, and method overloading, and many other points. All of the above resources were criticized and pointed out to the creators of Go, who responded: "These omissions were simplifications that contributed and contribute to the strength of Go." There are many things in the Go language and libraries that differ from modern practices, and it was a decision of the Go development team: "Sometimes it's worth trying a different approach."

Everyone is invited to help and contribute if they wish. Interested parties can open proposals for new features and everything related to the language ecosystem. The Go source code is available on GitHub. The documentation on how you can contribute to the language is provided here.

Concurrency is different from parallelism

One of the famous phrases is: *“Competition is about dealing with many things at the same time. Parallelism is doing many things at the same time”.*Concurrency is the composition of independently executing calculations. Concurrency is a way of structuring software, and it is definitely not parallelism although it does allow for parallelism.

If you only have one processor, your program can still be concurrent, but not parallel. On the other hand, a well-written concurrent program can run efficiently in parallel on a multiprocessor. I suggest checking out this video of a talk by Rob PikeConcurrency is not Parallelism”.

Concurrency in Go is very powerful and also simple to code, which was the intent of the engineers who developed Go. Solving many problems is much more efficient using concurrency and this is the power of Go, so it has become a god when it comes to concurrency. Due to this, problems encompassed in this universe will be solved very efficiently and most importantly, with very little computational resources.

The example below is a classic one when referring to concurrency, when we want to balance CPU and I/O we use this parttern Fan-out.

What is Fan-out ?

Multiple functions can read from the same channel until that channel is closed, this is called fan-out . This provides a way to distribute work among a group of workers to parallelize CPU and I/O usage.

/*
* Example Worker
* @package     main
* @author      @jeffotoni
 */

// In this example we'll look at how to implement
// a _worker pool_ using goroutines and channels.
package main

import "fmt"
import "time"

// Here's the worker, of which we'll run several
// concurrent instances. These workers will receive
// work on the `jobs` channel and send the corresponding
// results on `results`. We'll sleep a second per job to
// simulate an expensive task.
func worker(id int, jobs <-chan int, results chan<- int) {

    for j := range jobs {

        fmt.Println("worker", id, "started  job", j)

        time.Sleep(time.Second)

        fmt.Println("worker", id, "finished job", j)
        results <- j * 2
    }
}

func main() {

    // In order to use our pool of workers we need to send
    // them work and collect their results. We make 2
    // channels for this.
    jobs := make(chan int, 100)
    results := make(chan int, 100)

    // This starts up 3 workers, initially blocked
    // because there are no jobs yet.
    for w := 1; w <= 3; w++ {
        go worker(w, jobs, results)
    }

    // Here we send 5 `jobs` and then `close` that
    // channel to indicate that's all the work we have.
    for j := 1; j <= 5; j++ {
        jobs <- j
    }

    close(jobs)

    // Finally we collect all the results of the work.
    for a := 1; a <= 5; a++ {
        <-results
    }
}
Enter fullscreen mode Exit fullscreen mode

Good points of Go:

  • Does not have Generics;

  • Does not have operator overload;

  • Does not have pointer arithmetic;

  • Threads management is not explicit;

  • Has type inference;

  • Dependencies are clear;

  • The syntax is clean;

  • The semantics are clear;

  • Composition and not inheritance;

  • Native GC (Garbage collector);

  • Has Goroutines;

  • Native support for concurrent programming;

  • First Class Functions (we can pass functions as parameters);

  • Multiplataform;

  • Open Source;

  • Very low Learning Curve or easy to learn;

  • Highly productive;

  • Resolves scalability issues;

  • It can lowers your server costs drastically;

  • Several problems thought from the Go perspective become much easier to treat and solve them;

  • It has only 25 reserved words;

  • Multi-paradigm: concurrent, functional, imperative, object-oriented;

  • MemorySafe.

We know that several points above will be criticized, but in practice and over time we realized that all the omitted simplifications made Go an even more powerful language. For simple or complex projects, Go will always be an excellent alternative, as it makes everything simpler and leaner, easier to code and maintain.

Go is love

Top comments (0)