Appendix A – Go Garbage Collector

This Appendix is all about the Go Garbage Collector (GC). This crucial part of Go can affect the performance of your code more than any other Go component. We will begin by talking about the heap and the stack.

Heap and stack

The heap is the place where programming languages store global variables—the heap is where garbage collection takes place. The stack is the place where programming languages store temporary variables used by functions—each function has its own stack. As goroutines are located in user space, the Go runtime is responsible for the rules that govern their operation. Additionally, each goroutine has its own stack, whereas the heap is "shared" among goroutines.

In C++, when you create new variables ...

Get Mastering Go - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.