Important features and advantages of Go

Go comes with some important advantages for developers, starting with the fact that it was designed and is currently maintained by real programmers. Go is also easy to learn, especially if we are already familiar with programming languages such as C, Python, or Java. On top of that, Go code is good-looking, which is great, especially when we are programming applications for a living, and we have to look at code on a daily basis. Go code is also easy to read and offers support for Unicode out of the box, which means that we can make changes to existing Go code easily. Lastly, Go has reserved only 25 keywords, which makes it much easier to remember the language. Can we do that with C++?

Goroutines and channels

Go comes with concurrency capabilities using a simple concurrency model that is implemented using goroutines and channels. Go manages OS threads for us and has a powerful runtime that allows us to spawn lightweight units of work (goroutines) that communicate with each other using channels. Although Go comes with a rich standard library, there are really handy Go packages such as cobra and viper that allow Go to develop complex command-line utilities, such as docker and hugo. This is greatly supported by the fact that Go’s executable binaries are statically linked, which means that once they are generated, they do not depend on any shared libraries and include all required information.

Pointers and interfaces

Due to its simplicity, Go code is predictable and does not have strange side effects, and although Go supports pointers, it does not support pointer arithmeticPointer arithmetic refers to performing arithmetic operations (such as addition or subtraction) on a pointer. like C unless we use the unsafe package, which is the root of many bugs and security holes. Although Go is not an object-oriented programming language, Go interfaces are very versatile and allow us to mimic some of the capabilities of object-oriented languages, such as polymorphism, encapsulation, and composition.

Go generics and garbage collection

Additionally, the latest Go versions offer support for generics, which simplifies our code when working with multiple data types. Last but not least, Go comes with support for garbage collection, which means that no manual memory management is needed.

Although Go is a very practical and competent programming language, it is not perfect:

  • Although this is a personal preference rather than an actual technical shortcoming, Go has no direct support for object-oriented programming, which is a popular programming paradigm.

  • Although goroutines are lightweight, they are not as powerful as OS threads. Depending on the application we are trying to implement, there might exist some rare cases where goroutines will not be appropriate for the job. However, in most cases, designing our application with goroutines and channels in mind will solve our problems.

  • Although garbage collection is fast enough most of the time and for almost all kinds of applications, there are times when we need to handle memory allocation manually—Go can’t do that. In practice, this means that Go will not allow us to perform any memory management manually.

Use cases

However, there are many cases where we can choose Go, including the following:

  • Creating complex command-line utilities with multiple commands, subcommands, and command-line parameters

  • Building highly concurrent applications

  • Developing servers that work with APIs and clients that interact by exchanging data in myriad formats, including JSON, XML, and CSV

  • Developing WebSocket servers and clients

  • Developing gRPC servers and clients

  • Developing robust UNIX and Windows system tools

  • Learning programming

In the following chapters, we will cover a number of concepts and utilities in order to build a solid foundation of knowledge before building a simplified version of the which(1) utility. At the end of the chapter, we’ll develop a naive phone book application that will keep evolving as we explain more Go features in the chapters that follow.

But first, we’ll present the go doc command, which allows us to find information about the Go standard library, its packages, and their functions. Then, we’ll show how to execute the Go code using the Hello World! program as an example.