From Learn Go with Pocket-Sized Projects by Aliénor Latour, Pascal Bertrand, and Donia Chaiehloudj
This book is intended to get you up to speed with Go by using it in a series of immediately useful, small-scale projects. If you’re a developer who’s interested in learning Go and doing in a fast and practical way, read on!
Learning Go on the Go
This book aims to get the reader up and running using the Go language on the job, in the context of modern engineering, be it for personal curiosity, as part of a research exercise, or in the context of an industrial project.
We will not cover everything there is to know about the language, but focus instead on the main things we need, as developers, in order to be productive and efficient.
This book provides you with a set of fun projects to progressively explore the features of the Go language. Each pocket-sized project is written in a reasonable number of lines. Our goal is to provide various exercises so any developer who wants to begin with Go or to explore the language can follow the steps described in each chapter.
We want to help the reader become a good modern software developer by using the Go language. We will use our experience as software engineers to provide meaningful advice for newcomers and seasoned developers.
This book also contains tutorials for implementing APIs with microservices, demonstrating how the language is great for cloud computing. It finishes with a project that uses TinyGo, the compiler for embedded systems.
If you are a beginner at programming, we wholeheartedly suggest starting your Go experience with Get Programming with Go.
Let’s see why Go is a great investment of your learning time as a developer.
How and where can Go help you?
Go is a versatile language designed for maintainability and readability. It is optimal for backend software development and has great integration with modern cloud technologies.
Considering the average turnover in tech companies is getting lower every year, a little over a year today, it is important that code written by one person can be read by another after they leave the company. It is therefore crucial that the language chosen by the company aims for readability.
Go’s key features make it a reliable and secure language, with a fast build time.
Some applications leverage goroutines which are a safer and less costly way of dealing with parallel computing than threads. Threads rely on the OS which has a limit related to the size and power of the CPU, whereas goroutines happen at the application’s level. To make the stacks small, Go uses resizable, bounded stacks. A newly minted goroutine is given a few kilobytes, which is almost always enough, and can grow and shrink, allowing many goroutines to live in a modest amount of memory. It is practical to create hundreds of thousands of goroutines in the same address space.
Even though it is not an object-oriented language and has no inheritance system, Go supports most of the features via composition and implicit interfaces. The old argument of Go missing generics, making it verbose and requiring a lot of boilerplate copy paste, has come a long way towards becoming invalid since the famous 1.18 version. As we write, discussions are still in progress to make it as versatile as it is simple.
Go is a compiled language, meaning that all syntax errors will be found during compilation rather than at runtime. We all prefer to know about mistakes in the safety of our own computer rather than discovering them, say, in production.
It is easier to run applications at scale with Go than with many other languages. Go was built by Google to solve problems at a Google-sized scale. It is ideal for large concurrent applications.
Cloud platforms love Go. They provide support for Go as a major language. For example, cloud functions and lambdas support Go in all the most-used providers. Major cloud tools, such as Kubernetes or Docker, are written in Go.
Where can Go NOT help you?
Despite its high versatility, there are few use cases that Go is not made to cover.
Go relies on a garbage collector to release the memory it uses. If your application requires full control over memory, pick a lower-level language like the C family can provide. Go can wrap libraries written in C/C++ with CGo, a translation layer created to ease the transition between the 2 languages. With this CGo twist, you can wrap dynamically-linkable libraries.
The Go toolchain mostly produces executables – generating a Go-compiled library is painfully achievable. We won’t cover it in this book. In most common cases, updating the version of a Go dependency implies rebuilding the binary with that new version. This also means that, in order to use a Go library, you need to have access to its source code.
The Go compiler supports an interesting list of different platforms and operating systems, but we wouldn’t recommend writing an operating system with Go, although many brave souls have done it. The main reason for this is the handling of the memory as it is done in Go: the garbage collector regularly discards bits that are no longer used. As with all garbage collectors, it is adjustable, but it won’t release memory exactly how you want or when you want.
Go binary files are known to be bigger than average. It is usually not a problem in a Cloud environment, but if you require light binaries, consider using the TinyGo compiler.
Last, the difficulty to google up answers – seriously, who names their language with such a common word? Apparently, Google themselves. Here’s a pro tip: when trying to find an answer, use “golang”, which is not the real name but is what search engines will recognize. Sometimes it’s like trying to find documentation in C on strings – you don’t get what you were expecting.
We can also mention the difficulty in hiring Go developers, which is, actually, good for us developers.
Why Pocket Projects?
At the end of the XIXth century, scientists started to theorize learning. Among them, John Dewey wrote in 1897 a long list of good reasons why doing is the best way to learn. Since then, experience has proven his claims in many education systems and learning situations.
The projects that we propose here are timed for busy people. We made sure to keep them as small as possible while still making them rewarding. We admit that some of them are not particularly fun, but these are the most useful in a real-world project.
Whom this book is for
First and foremost, we are writing for developers who know and use another language and would like to extend their professional skills. We want to get you up and running by sharing practical usage of the language.
We focus on industry standards, including long-term considerations and not just a tiny project’s throwaway code that you don’t bother to test.
We are also thinking about teams who are evaluating Go for their next project. Diving into Go’s main features will help you decide if it’s the best language you can possibly invest in.
What you will know after reading the book (and writing the code)
First, we want to make sure that you understand what the book explains. For this matter, we will guide you through our journey of chapters describing the implementation of the current code iteratively, on a commit-by-commit basis, as we consider it important to understand what’s happening bit by bit.
Second, we aim at providing good and clear examples for writing industry-level Go code – recommendations that apply outside our examples, and that will help you venture into the real world of development. All of our examples contain functions that are reusable in a company.
Last, our goal is to make you realize that you can write excellent Go code by yourself once you’ve understood the basics.
We start at the Hello-World level, discovering the syntax of the language, and progress all the way to a service ready to be deployed in the Cloud, walking you through architectural decisions.
Grammar and syntax
The first chapters focus on the grammar specific to Go. For example, how all loops start with the same keyword, breaks are implicit in switches, but also how to expose (and not) some of your constants and methods (what Java calls public or private).
What Go chose in its code design was to make its interfaces implicit. In most of the other big languages, in order to have one entity (or class) be considered as implementing an interface, it needs to explicitly state so in its definition. In Go, implementing the methods is enough. You can therefore unknowingly implement an interface that you don’t know yet. This opens worlds of new possibilities in how we envision mocking and stubbing, dependency injection, and interoperability.
Even though goroutines are a great feature of Go, we won’t dwell on them. In our experience, you can program efficiently in Go without them. Only one project uses them.
Finally, as you will learn throughout the book, Go does not use exceptions. It prefers to consider errors as values. This changes the way we deal with flows that don’t follow the happy path, where nothing ever fails. Every program has to deal with errors at some point, and we’ll cover this throughout the projects.
Testing your code
Each and every chapter includes unit testing.
All of them.
No developer today would dream about delivering code in production that is not covered by at least some tests, whatever their level. They are indispensable for any evolution the software grows through. That is why we include unit tests everywhere.
Go is also great at benchmarking different algorithms with a built-in bench command. It allows developers to compare versions too, which means you can use it to check on every commit that your code-level performance does not decrease. You will see a few examples throughout the book.
One last recent feature of the Go tooling test chain is fuzzing. Fuzzing is a way to test a system by throwing random values at it and seeing how it behaves. It is a great help in checking for vulnerabilities.
Clean code best practices
While the first few projects fit in one file, we’ll quickly need to organize the code in a way that makes it easy to maintain. By maintain, we mean that it should allow a newcomer to find their way through your code in order to fix a bug or add a feature. This fictional newcomer could be you in a very short time.
We suggest and explain some code organization practices. We believe that Go is great for domain-driven design and organize our code accordingly. There is of course no single folder organization for a Go project, but we aim for what makes more sense.
What to expose and what to keep for yourself has tormented humanity for millennia, and software developers for decades. This question is covered as soon as we create something that goes beyond one package.
As Go is mostly used for writing services deployed in cloud environments, we added two projects to help you pick your favorite protocol: one serves HTML over HTTP, the other uses protobuf over gRPC. You will write fully-functioning services that you can easily deploy to play around and see what you prefer and what best fits your needs.
Once they are running, you need to monitor what happens in your program. One of the early and easy projects is a logger that goes beyond what the default standard library does. Another one reads an API and acts as an anti-corruption layer to insert the data from that API into your domain. A third one is a simple load balancer for your system’s traffic, that you can make more complex according to your needs.
IoT is fun
The last project is designed to run on an Arduino microcontroller, using a different compiler. It is not enough to make you an embedded systems expert. It will just play as an intro to some of the lesser-known features of Go. We hope to tickle your creativity and hope you will enjoy it all.
- Go is a modern, industry-oriented, simple and versatile language, best for backend development, widely used for cloud-oriented tools, great for CLI and even adapted to embedded systems.
- Easy to learn, teams are quickly efficient with it, writing complicated frustrating code is possible but not easy.
- This book: learn by doing pocket projects that only take a few hours, gets you up and running.
Learn more about the book here.