If I have a go module that has three package like A, B, C. In the main.go and all of its import, only A, B packages are ever used. My question is, does the binary produced by go build
has any code from package C?
Asked
Active
Viewed 48 times
1 Answers
1
The binary build will only include the transitive closure of all symbols referenced from main. This will include only those functions and data from the imported packages, and all the methods of types that are used. So if you have a function in a package that is never used, that will not be in the binary. However if you use a data type with unused methods, those methods will be in the binary.

Burak Serdar
- 46,455
- 3
- 40
- 59
-
The linker may even exclude methods from the binary if certain language features (e.g. reflection) are not used. See https://go.dev/blog/go1.7-binary-size – icza Jan 05 '23 at 07:04