How to learn Golang: A DevOps engineer's experience
While some languages don't provide much proprietary support for new learners, Go makes it simple for anyone interested in the language to pick up the basics in a couple hours.
Born from criticism of C++ and other similar languages, Go was created in 2007 by software engineers at Google. Also called Golang, Go aimed to increase programmer productivity by offering a better programming experience than popular languages in use at the time.
Google's goal was to create a compiled, statically typed, readable and writable language that performed well -- specifically in the area of concurrency and high-performance networking. Improving on older languages created before the advent of multicore processors, Go adds built-in concurrency with goroutines and Go channels.
Go's design also offers other developer conveniences, such as garbage collection, a built-in package manager and simple cross-platform compilation. For both new and seasoned programmers alike, Go is a well-suited choice with many thoughtfully designed modern features and high-quality learning resources.
How to get started
Go has thorough documentation that provides a variety of resources, such as interactive tutorials, proper documentation references and in-depth articles to explain specific Go functions.
One option is A Tour of Go. The interactive tutorial enables students to execute Go examples right alongside the instructions, quickly putting lessons into practice. This tutorial offers exercises that challenge learners to create a function or solve a problem using the features explained in the previous lessons. To complete these exercises, students must apply new concepts and demonstrate a more thorough understanding than just having read and executed the provided examples.
Why learn Go?
Go channels are a data structure for goroutines to intercommunicate efficiently, despite running asynchronously with each other and the rest of normal program flow. Go channels block operations within a goroutine until data is either sent or received. This enables IT admins to "background" operations in one goroutine until an operation is completed in a program or another goroutine.
Because Go is a strongly typed language, it has a leg up on languages like Python, which can be difficult to read sometimes due to support for dynamically typed variables and functions.
Figures 1 and 2 compare two functions in Python and Go, showing the differences in readability.
Python is easy to write, as coders can omit the specific types of the arguments x and y, but calling the functions is a bit more complicated. A user could call addNumbers with two strings and it would still be valid, returning a concatenation of the two provided strings. This indeterminate behavior based on argument types can make reading some Python code confusing.
In Go, users must provide types for the arguments, as well as a return type for the function. This approach enables quick understanding that the arguments being passed to the addNumbers function in Go are integers.
While some languages only offer a programming language without built-in support for package management or testing libraries, Go is much more generous. Go comes with a command-line tool that enables users to build, test, package and install Go programs. Rather than rely on third-party tools to support common actions, such as downloading dependencies for a project or running tests on code, the Go command-line tool supports all these actions. The Go command-line tool even offers a static code analyzer, which runs with the vet subcommand, to look for potential mistakes in code.
Starting with a poorly formatted main function, as Figure 3 shows, running the go fmt command will automatically adjust the spaces and new lines.
After running the go fmt command, the spaces and lines will adjust to look like Figure 4.
Combine this with a CI script to run go fmt every time a developer pushes code to enforce cleanliness and readability across all Go files in a project.
Go has many creature comforts to support developers writing quality code. Choose Go for a project where speed, concurrency and overall developer experience are prioritized. With conventions to support readable code, extensive documentation and an included command-line tool, Go has many advantages over other modern languages.