A Quick Introduction to Go

Introducing Go

Imagine that you are a developer and you want to create a command-line utility. Similarly, imagine that you have a REST API and you want to create a RESTful server that implements that REST API. The first thought that will come to your mind will most likely be which programming language to use.

The most common answer to this question is to use the programming language we know best. However, this course is here to help us consider Go for all these and many more tasks and projects. In this chapter, we begin by explaining what Go is, the history of Go, and how to run Go code. We will explain some core characteristics of Go, such as how to define variables, control the flow of our programs, and get user input, and we will apply some of these concepts by creating a command-line phone book application.

We will cover the following topics:

  • Introducing Go

  • Hello World!

  • Running Go code

  • Important characteristics of Go

  • Developing the which(1) utility in Go

  • Logging information

  • Overview of Go generics

  • Developing a basic phone book application

Genesis and official title

Go is an open-source systems programming language initially developed as an internal Google project that went public back in 2009. The spiritual fathers of Go are Robert Griesemer, Ken Thomson, and Rob Pike.

Note: Although the official name of the language is Go, it is sometimes (wrongly) referred to as Golang. The official reason for this is that go.org was not available for registration, and golang.org was chosen instead. The practical reason for this is that when we are querying a search engine for Go-related information, the word “Go” is usually interpreted as a verb. Additionally, the official X (previously known as Twitter) hashtag for Go is #golang.

Application of the Go language

Although Go is a general-purpose programming language, it is primarily used for writing system tools, command-line utilities, web services, and software that work over networks. Go can also be used for teaching programming and is a good candidate as our first programming language because of its clear ideas, principles, and lack of verbosity. Go can help us develop the following kinds of applications:

  • Professional web services

  • Networking tools and servers such as Kubernetes and Istio

  • Backend systems

  • System Utilities

  • Powerful command-line utilities, such as docker and hugo

  • Applications that exchange data in JSON format

  • Applications that process data from relational databases, NoSQL databases, or other popular data storage systems

  • Compilers and interpreters for programming languages we design

  • Database systems such as CockroachDB and key/value stores, such as etcd

Note: Go will not allow the program to get executed if there are any unused imports or variables. 

There are many things that Go does better than other programming languages, including the following:

  • The default behavior of the Go compiler can catch a large set of silly errors that might result in bugs.

  • Go uses fewer parentheses than C, C++, or Java and no semicolons, which makes the look of Go source code more human-readable and less error-prone.

  • Go comes with a rich and reliable standard library.

  • Go has support for concurrency out of the box through goroutines and channels.

  • Goroutines are really lightweight. We can easily run thousands of goroutines on any modern machine without any performance issues.

  • Unlike C, Go supports functional programming.

  • Go code is backward compatible, which means that newer versions of the Go compiler accept programs that were created using a previous version of the language without any modifications. This compatibility guarantee is limited to major versions of Go. For example, there is no guarantee that a Go 1.x program will compile with Go 2.x.

The history of Go

As mentioned earlier, Go started as an internal Google project that went public back in 2009. Griesemer, Thomson, and Pike designed Go as a language for professional programmers who want to build reliable, robust, and efficient software that is easy to manage. They designed Go with simplicity in mind, even if simplicity meant that Go was not going to be a programming language for everyone.

The figure that follows shows the programming languages that directly or indirectly influenced Go. As an example, the Go syntax looks like C, whereas the package concept was inspired by Modula-2.

The programming languages that influenced Go
The programming languages that influenced Go

The deliverable was a programming language, its tools, and its standard library. What we get with Go, apart from its syntax and tools, is a pretty rich standard library and a type system that tries to save us from easy mistakes, such as implicit type conversions, unused variables, and unused packages. The Go compiler catches most of these easy mistakes and refuses to compile until we do something about them.

Note: Additionally, the Go compiler can find mistakes that are difficult to catch, such as race conditions.

If we want to use the Go language, we have to install it first. However, there is a big chance that your UNIX variant has a ready to-install package for the Go programming language, so you might want to get Go by using your favorite package manager.

Why UNIX and not Windows?

The question that arises here is, why are we talking about UNIX all the time and not discussing Microsoft Windows as well? There are two main reasons for this. The first reason is that most Go programs will work on Windows machines without any code changes because Go is portable by design—this means that we shouldn’t worry about the operating system we’re using.

However, we might need to make small changes to the code of some system utilities for them to work in Windows. Additionally, there are still going to be some libraries that only work on Windows machines and some that only work on non-Windows machines. The second reason is that many services written in Go are executed in a Docker environment. Docker images use the Linux operating system, which means that we should program our utilities with the Linux operating system in mind.

Note: As far as user experience is concerned, UNIX and Linux are very similar. At its inception, UNIX was proprietary software and provided much of the inspiration for open-source operating systems like Linux.