Why are my Go builds using so much space? With gobuildsize you can extract package-level data on the sizes of archive files fed to the linker.
Suppose you have a regular Go build:
go build "$FLAGS"
Substitute this command with the following:
gobuildsize "$FLAGS"
This will print out byte sizes of archive files that Go compiler creates for each Go package participating in the build. For a quick example, gobuildsize can measure itself:
go build ./cmd/gobuildsize
./gobuildsize ./cmd/gobuildsize | head -n 10
runtime | 11772860 |
reflect | 2938266 |
syscall | 1799498 |
time | 1319664 |
internal/abi | 1180960 |
os | 1130052 |
fmt | 958316 |
regexp/syntax | 944206 |
regexp | 809204 |
internal/reflectlite | 801036 |
Note that Go will aggressively eliminate unreachable package from the final executable. Therefore, if a packages shows up in the gobuildsize report it certainly contributes to the build-time resource usage and takes space in the cache, but it does not necessarily contribute to the final statically linked executable bloat. This report is therefore more about understanding slow builds and controlling resources in constrained CI environments than optimizing the size of the final executable.