Go Build Cache

From NovaOrdis Knowledge Base
Jump to navigation Jump to search

Internal

Overview

The Go build cache is a filesystem storage area used by the Go compiler to store source file hashes and object files produced by builds. This data allows the Go compiler to implement incremental builds. The build cache is different from the module cache, which stores module source code and metadata.

On a Mac, the build cache resides under /Users/<user-name>/Library/Cache/go-build, where /Users/<user-name>/Library/Cache is the standard user cache directory for MacOS. On a Linux system, it is ~/.cache/go/go-build. The location is specified by the GOCACHE environment variable, which can be printed with:

go env GOCACHE

The cache can be cleaned with go clean.

Structure

The go-buid folder contains 256 subfolders named after hexadecimal strings from 00 to FF. These subfolders store intermediate files, which allows the compiler to parallelize the compilation process by handling different files in different folders. The Go compiler divides the source code into chunks. Each chunk is compiled into separated folders. The name of each file is given thy the SHA-256 hash code of the source code of the relative chunk. The hash function takes the source code chunk as input and produces a unique output. The files with an -a suffix are used to store compiled object files. The files with an -d suffix are intended for storage of debugging information.

Organizatorium

What is the relationship with the module cache?

Fuzz Cache

Can be cleaned with go clean -fuzzcache.