2.7 Go Runtime#
Although the code generated by the Go compiler is native executable code, this code still runs within the Go runtime (which can be found in the runtime
package). This runtime is similar to the virtual machines used by Java and .NET languages, and it is responsible for managing tasks such as memory allocation, garbage collection (Section 10.8), stack handling, goroutines, channels, slices, maps, and reflection.
The runtime is primarily written in the C language (self-hosted since Go 1.5) and is the top-level package for every Go package. You can find related content in the directory $GOROOT/src/runtime
.
Garbage Collector Go has a simple yet efficient mark-and-sweep garbage collector. Its main idea comes from IBM's reusable garbage collector and aims to create an efficient and low-latency concurrent collector. Currently, gccgo does not have a garbage collector, and a new garbage collector that works with both gc and gccgo is under development. Using a programming language with garbage collection does not mean that you can avoid the issues caused by memory allocation. Both allocation and deallocation of memory are CPU-intensive operations.
Go executable files are generally larger than their corresponding source code files, which indicates that the Go runtime is embedded in each executable file. Of course, when deploying to a large cluster, the larger file size can be a headache. However, overall, the deployment of Go is much easier compared to Java and Python. This is because Go does not require any other files as dependencies; it only needs a single static file, so you won't have the confusion of dealing with different versions of dependency files like you would with other languages.