Everyday Golang - The Fast Track
Everyday Golang - The Fast Track
Alex Ellis
2021
2021 OpenFaaS Ltd
Introduction
Everyday Go is the fast way to learn tools, techniques and patterns from real tools used in production.
About the author: Alex is a software engineer and an avid technical writer. Theres nothing he likes more than learning a new technology, and then writing it up in a clear, practical way. Hes well known for his blog posts on containers, Kubernetes, cloud computing and Go. He founded the OpenFaaS project in 2016, and since then has gone on to publish training courses, eBooks and dozens of other tools for developers.
How to use the book
This book is a compilation of practical examples, lessons and techniques for Go developers. They are the kind of things that you may need to learn and apply in your everyday journey. The topics cover the software lifecycle from learning the fundamentals, to software testing, to distribution and monitoring.
If you feel like you may have seen some of these techniques before, you would be right. Ive been writing about Go since 2015 and wanted to take everything that Ive needed on a regular basis, and to lay it out for you to benefit from. Its not a reference book or a complete guide to the language, but if you like learning by example and arent afraid to get your feet wet, then this style is probably for you.
The chapter Writing unit-tests in Go was originally part of my top ranking and performing blog post. It was even featured in the Kubernetes documentation to help new Go developers get up to speed. In Everyday Go, Ive rewritten, updated and extended the tutorial with more content and techniques that you can use in your work.
You could say that this is the fast track to get the results youll need in your everyday writing of applications in Go.
- How to use the book
- Why Go?
- Getting started with Go
- Managing packages and paths
- Understand Go modules and vendoring
- Creating your first program in Go
- Adding external dependencies to your program
- Cross-compile your code for different Operating Systems
- Inject version information into your binaries
- Merge Go objects together into one
- Deal with HTTP and JSON parsing
- Learn how to use concurrency with Goroutines, channels, Mutexes, error groups and WaitGroups
- Avoid a common mistake with Goroutines
- Write unit tests and understand everything thats available to you
- Learn party tricks of the testing in Go
- Isolate dependencies and unit test a HTTP server
- Embed data and files into your application
- Create dynamic content with templates
- Build a CLI that youll enjoy using
- Load and write to config files in YAML
- Write your own HTTP microservices
- Integrate with Prometheus for metrics
- Learn to build and release Go binaries
- Build Docker container images
- Release your code on GitHub
- Get set for your onward journey
Help the book to evolve
Just as my knowledge of Go has evolved over time, this book will also evolve with updates and new examples. This book should be a place that you can come to for a pattern or sample that you can use as a basis of a new program or feature.
Check the final chapter for how to submit comments, suggestions or corrections.
Changelog
- 5 August 2021 - Clarify usage of RWMutex and locking behaviour
- 19 July 2021 - Fix typo on X509 certificates.
- 14 July 2021 - Correct git clone URL, explain Marshal and Unmarshal, remove an import.
- 28 June 2021 - Update wording on concurrency vs parallelism, introduce new locking example and Mutex/RWMutex.
- 27 June 2021 - Update to Go modules section and clarification of paths in package cache directory
- 24 June 2021 - MIT license added for example apps
- 24 June 2021 - Add section on multiple packages and new example app multiple-packages
- 23 June 2021 - Add new section on Go modules usage and vendoring
- 22 June 2021 - Update Go release date in intro, add merge-objects example app
Why Go?
Gos new logo
According to Wikipedia, Go (or Golang) is a programming language that was developed by a team at Google and first appeared in 2009. Its synonymous with server-side development and tooling for infrastructure projects.
Go is an open source programming language that enables the production of simple, efficient and reliable software at scale
Here are some projects that come to mind, which are entirely written in Go:
- Docker - a container engine written by Docker Inc
- Kubernetes - a clustering system for containers built by Google and open source contributors
- Traefik - a load-balancer built by Traefik Labs
- Consul, Terraform - tools for managing infrastructure built by Hashicorp
- Caddy - a high performance HTTP server written by Matt Holt
- OpenFaaS - serverless framework for hosting your own functions, built by OpenFaaS Ltd and open source contributors
These are just a few of hundreds of thousands of projects which have appeared on GitHubs trending page for Go.
As you can see from the above, Go is very popular for server-side projects, but it is also very popular for writing Command Line Interfaces (CLIs) because the binaries can be statically linked. Statically-linked Go binaries are both fast and small, and do not require an additional runtime making them a very popular choice.
My personal highlights for Go are:
- portability - static compilation for easy distribution
- cross-compilation - being able to build binaries for Linux, Windows, MacOS Operating Systems (OS) and Arm computers with a single command (
go build
) - concurrency model - Go has built-in mechanisms for concurrency that make it well suited to tasks both small and large
- ecosystem - knowing Go means being able to contribute to almost any other open source Go project or product
- tooling - Go includes its own unit-testing runner and profiler, both of which are simple and easy to use
- opinions - Go has strong opinions on formatting, style and typing that make it easy to move between codebases
Above everything else, Go has an impressive set of packages available within its standard libraries or stdlib. There are many useful packages and utilities that would need to be installed separately in a programming language like JavaScript, Ruby or Java.
crypto
- libraries for X.509 certificates and cryptographycompress
- work with zip files and archiveshttp
- a very powerful and simple HTTP client and server package including things like reverse proxiesnet
- work directly with sockets, URLS and DNSencoding/json
- work directly with JSON filestext/template
- a powerful templating engine for replacing tokens in files, and generating text and HTMLos
- work with low level OS primitives
You can find a complete package list here.
Through the next few chapters we will explore some of the above highlights and also solve various practical problems. Even if youre a seasoned Go developer, Id recommend going through each of the exercises, because even after 5 years I still learn something new each week.