Why Should Devops/SRE learn Golang?
Tags: devops, sre, go, cloudnativeapplication
Published: 2024-03-31 08:11:27

## Why Golang?
Go was created to specifically answer the following objectives:

Concurrency: Go has built-in concurrency support, which makes it easier to write concurrent and parallel programs. Other languages like Java has concurrency support as well, but it can be more complex and require more code to achieve the same results.

Scalability: To satisfy the needs of contemporary distributed systems, Go was created to be able to manage big codebases and scale horizontally.

Performance: Go was designed with performance in mind, This is partly due to Go's garbage collector, which is optimized for low-latency and high-throughput scenarios.

Lightweight threads: goroutines and integrated concurrency mechanism make it simple to construct concurrent and parallel programmes.

Efficiency: Go's garbage collector was designed to produce quick and efficient code in low-latency, high-throughput applications.

Simple syntax: Go was created with an emphasis on readability and maintainability and a short, uniform language syntax to make it simple to learn and use.

Cross-platform compatibility: Go was made to compile to a variety of platforms, making it simple to create programmes that work across several operating systems and architectures.

Deployment: Go produces a single binary file that is self contained. Which means this binary can be easily deployed to different platforms without dependencies. This makes it easier to distribute and deploy Go applications.

**Now traditionally we can support concurrency using multiple ways like multiprocessing, multithreading,etc but the popular choice is multithreading**


ex:The following companies uses Golang to support concurrency.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jxnp5s0lzi1v7omkhuzv.PNG)

##Self Contained Binary: The Go compiler, which is a component of the Go installation, takes your source code and creates a binary executable file with all the required libraries and dependencies when you compile a Go program. The destination system does not need to have the Go installation or any other dependencies in order for this binary file to run because it is self-contained.

Because to this, it is simple to distribute and run Go applications across several platforms without worrying about the target system's Go version or any other requirements.

## Better than Java ?

Golang is better than Java in few scenarios only. It is better to evaluate your use-case before choosing any option.

The advantages of using golang is well described in the above section(Why Golang?). However, Java on the other hand is a very mature programming language in its own ways.

Java has a strong type system, which can help catch errors at compile time.

Strong enterprise frameworks

Large community compared to Golang.

So, it is not completely correct to say Golang is better than Java or Java is better than Golang.

## Why DevOps Applications are written in Golang?
It is a known fact that popular devops applications such as Kubernetes, Docker and many are written in Golang. But why ?

As Go was created with concurrency in mind, writing concurrent and parallel programs is made simpler. Applications for DevOps, which frequently need to handle numerous containers or processes at once, should pay special attention to this. This is one of the primary reasons why Golang is a popular choice in DevOps community.

## Why is multi-threading so popular in golang ?

A: Most of the programming languages create kernel-level threads, which incurs a large amount of overhead and may lead to expensive context switches. Whereas in Go the threads are user-level. The Go runtime, which is built in Go and offers a lightweight scheduler that can effectively handle dozens or even millions of Go routines.

Also the fact that Go routines share an address space and heap makes the routines light weight because there is no need to duplicate data between threads or processes in order for them to communicate and share information.

Because of the above reasons, writing concurrent code in Go is simple and doesn't need thinking about problems like deadlocks, race situations, or memory synchronization.

##The difference between user-level and kernal-level threads ?

A: User-level threads and kernel-level threads are two different approaches to implementing threads in an operating system. Here is a detailed difference between the two:

Management: User-level threads are managed by the user-level thread library that is implemented in the programming language, while kernel-level threads are managed by the operating system kernel.

Scheduling: User-level threads are scheduled by the user-level thread library, which uses its own scheduling algorithm and decides which thread to run next. Kernel-level threads are scheduled by the operating system kernel, which uses its own scheduling algorithm and decides which process or thread to run next.

Context Switching: In user-level threads, context switching is done entirely in user space, which means that switching between threads does not involve a context switch to the kernel. This makes context switching faster and more efficient, but it also means that user-level threads cannot take advantage of kernel-level features like hardware interrupts, which can cause delays in I/O operations. In kernel-level threads, context switching involves a context switch to the kernel, which makes it slower and more expensive, but it allows kernel-level threads to take advantage of hardware interrupts and other kernel-level features.

Resource Allocation: User-level threads are allocated resources by the user-level thread library, which means that the library can allocate resources based on its own policies and priorities. Kernel-level threads are allocated resources by the operating system kernel, which means that resource allocation is based on the kernel's policies and priorities.

Scalability: User-level threads are generally more scalable than kernel-level threads because they can be implemented with a lightweight thread library that does not rely on the operating system kernel. This makes it possible to create and manage thousands or even millions of user-level threads in a single application. In contrast, kernel-level threads require more resources and overhead, which can limit scalability.