snorberhuis
snorberhuis

Reputation: 3286

Will go test code only referenced in test files be compiled into the binary?

I am wondering what code will be compiled into the go binary if you compile a binary using go build ./... . This will compile a binary that has a cli program. For this cli program, I have test code and non test code. I currently have several flavours of test code:

No test code is actually referenced in the non test code. The testutil functions are only imported in the test files.

If the test code is infact compiled into the binary , how much of a problem is this?

Upvotes: 15

Views: 6977

Answers (2)

user3014416
user3014416

Reputation: 95

I believe that if you have an init() function in an otherwise unreachable file, it will still be linked into the executable.

_test.go files would be still excluded.

This bit us when we had some test helper code that was not in _test files. One had an init() function which ran on the executable startup.

Upvotes: 7

Adrian
Adrian

Reputation: 46532

A go binary only includes code reachable from its main() entry point. For test binaries main() is the test runner.

As to "how much of a problem" it is if it were included... none. It would increase the binary size and compilation time somewhat but otherwise have no impact - code that isn't executed, by definition, does nothing.

Upvotes: 15

Related Questions