Skip to Content
Efficient Go
book

Efficient Go

by Bartlomiej Plotka
November 2022
Intermediate to advanced
502 pages
13h 50m
English
O'Reilly Media, Inc.
Content preview from Efficient Go

Chapter 11. Optimization Patterns

With all we’ve learned from the past 10 chapters, it’s time to go through various patterns and common pitfalls I found when developing efficient code in Go. As I mentioned in Chapter 10, the optimization suggestion doesn’t generalize well. However, given you should know at this point how to assess code changes effectively, there is no harm in stating some common patterns that improve efficiency in certain cases.

Be a Mindful Go Developer

Remember that most optimization ideas you will see here are highly deliberate. This means we have to have a good reason to add them as they take the developer’s time to get right and maintain in the future. Even if you learn about some common optimization, ensure it improves efficiency for your specific workload.

Don’t use this chapter as a strict manual but as a list of potential options you did not think about. Nevertheless, always stick to the observability, benchmarking, and profiling tools we learned in previous chapters to ensure the optimizations you do are pragmatic, follow YAGNI, and are needed.

We will start with “Common Patterns”, where I describe some high-level optimization patterns we could see from optimization examples in Chapter 10. Then I will introduce you to the “The Three Rs Optimization Method”, an excellent memory optimization framework from the Go (and Prometheus) community.

Finally, in “Don’t Leak Resources”, “Pre-Allocate If You Can”, “Overusing Memory with Arrays”, and “Memory Reuse ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Go in Action

Go in Action

Brian Ketelsen, Erik St. Martin, William Kennedy
Learning Go

Learning Go

Jon Bodner
The Art of Clean Code

The Art of Clean Code

Christian Mayer

Publisher Resources

ISBN: 9781098105709Errata Page