DevOps Tools - Golang for DevOps Automation
What is Go?
The Go programming language (also known as Golang) is Google’s general-purpose programming language engineered for the multi-core reality of today’s computers. Go is a statically typed, compiled language like C++ that boasts the user friendliness of dynamically typed, interpreted languages like Python. Concurrent, garbage-collected, and designed for scale, Go is a programming language made for building large-scale, complex software.
Using Go in DevOps
Looking at recent observability and operations tools, many are written in Go like Terraform, Docker, Kubernetes or Prometheus. But why Go?
Go is not a language traditionally used in SysOps. However, as SysOps transforms to DevOps and systems complexity keeps increasing, the need for scalability is increasing as well.
Scalable systems need generalized support, less scripting, and more software development, ideally using a cross-platform language that supports concurrency and parallelism. This is a great time to refresh the toolbox. Looking at recent observability and operations tools, many are written in Go: Terraform, Docker, Kubernetes, Prometheus, CoreOS, Istio, Grafana, Jaeger, Moby, etc.
GO’s CORE FEATURE SET
Here is a broad overview of the major features this language has to offer:
- Compiled. Go generates binaries for your applications complete with all dependencies. No interpreters or runtime installations required.
- Garbage collection. Go sports low-latency garbage collector for automatic memory management that is both efficient and concurrent.
- Safe. Go is a statically typed language that supports type safety; errors are caught by the compiler before execution.
- C-inspired syntax. Go’s syntax is reminiscent of the C family, but simplified with only 25 keywords, the ability to parse without type information/symbol tables, and a Pascal-like declaration scheme.
- Multi-paradigm. Go supports multiple programming paradigms including imperative, object-oriented programming (OOP) without inheritance, and functional programming.
- Standard library. Go has a powerful standard library of packages to support the development of Go programs.
- Simplified documentation. GoDoc is a static code analysis tool that creates simplified standardized documentation directly from your code.
- Concurrent. Go treats concurrency as a first class citizen—we’ll dive into more detail in the next section.
Goroutines and channels: concurrency for a multicore world
Concurrency is the ability to deal with lots of things at once, such as a web server handling multiple requests. Concurrency is especially useful for building software that can take full performance advantage of multiple cores or process threads. One of Go’s biggest selling points is that it supports two features that make concurrency easy out of the box: goroutines and channels.
Goroutines are functions that can run concurrently with others methods and functions. In contrast with parallelism, where functions can execute simultaneously, concurrency simply means that a program does not need to wait for a function to complete in order to move on to the next task. While parallelism is certainly possible, it is not the goal. Concurrency is about structure.
If you’re familiar with threads in Java, goroutines are lighter and easier to create. Goroutines are:
- Fast: quicker startup times than conventional threads
- Lightweight: only a few kBs in stack size, allowing you to run millions
- Flexible: growable, segmented stacks that only consume memory as required
- Easy-to-control: channels let two or more goroutines coordinate and synchronize their execution
Invoked with the “go” keyword, these functions will start and continue to run while the program moves on to the next statement in your code, without having to wait for the goroutine to finish. In a nutshell, Go lets you make the most of your hardware, by making it easy to design programs that can run concurrently across multiple cores.
Composition over Inheritance
Sometimes you can tell a lot about a language by the features its creators choose to omit. Go deviates from most OOP languages by choosing composition over inheritance. Instead of subclasses and type-based inheritance, Go allows methods on any type via nifty constructs called interfaces. An interface is a set of methods: fixed behaviors that can be invoked as required. When a type implements its methods, the interface is implemented implicitly—no explicit declaration of intent required.
Select the language of your preference