This book offers practical tricks, tips, and suggestions to optimize Go code performance. Its insights are grounded in the official Go compiler and runtime implementation.
Life is full of trade-offs, and so is the programming world. In programming, we constantly balance trade-offs between code readability, maintainability, development efficiency, and performance, and even within each of these areas. For example, optimizing for performance often involves trade-offs between memory savings, execution speed, and implementation complexity.
In real-world projects, most code sections don't demand peak performance. Prioritizing maintainability and readability generally outweighs shaving every byte or microsecond. This book focuses on optimizing critical sections where performance truly matters. Be aware that some suggestions might lead to more verbose code or only exhibit significant gains in specific scenarios.
The contents in this book include:
This book neither explains how to use performance analysis tools, such as pprof, nor tries to study deeply on compiler and runtime implementation details. The books also doesn't introduce how to use profile-guided optimization. None of the contents provided in this book make use of unsafe pointers and cgo. And the book doesn't talk about algorithms. In other words, this book tries to provide some optimization suggestions in a way which is clear and easy to understand, for daily general Go programming.
Without particularly indicated, the code examples provided in this book are tested and run on a notebook with the following environment setup:
go version go1.23.2 linux/amd64
goos: linux
goarch: amd64
cpu: Intel(R) Core(TM) i5-4210U CPU @ 1.70GHz
Some benchmark times information is removed from benchmark results, to keep benchmark lines short.
Please note that:
Tapir is the author of this book. He also wrote the Go 101 book. He is planning to write some other Go 101 series books. Please look forward to.
Tapir was ever (maybe will be again) an indie game developer. You can find his games here: tapirgames.com.
During writing this book, the tool GoTV is used to manage installations of multiple Go toolchain versions and check the behavior differences between Go toolchain versions.
Welcome to improve this book by submitting corrections to Go 101 issue list (https://github.com/go101/go101) for all kinds of mistakes, such as typos, grammar errors, wording inaccuracies, wrong explanations, description flaws, code bugs, etc.
It is also welcome to send your feedback to the Go 101 twitter account: @zigo_101 (https://twitter.com/zigo_101).
The Go 101 project is hosted on Github. Welcome to improve Go 101 articles by submitting corrections for all kinds of mistakes, such as typos, grammar errors, wording inaccuracies, description flaws, code bugs and broken links.
If you would like to learn some Go details and facts every serveral days, please follow Go 101's official Twitter account @zigo_101.