Go: Unknown Parts of the Test Package
go test
command is probably the command people use the most in Go. However, there are some interesting details or usage you might not know about. Let’s start with the testing itself.
Idiomatic way to skip the cache
In Go, if you run your tests twice in a row, you can see that is actually running only once if they all pass at the first attempt. Indeed, the tests use a cache system so they don’t re-run the tests that did not change. Let’s try with running the tests of the math package:
root@91bb4e4ab781:/usr/local/go/src# go test ./math/
ok math 0.007s
root@91bb4e4ab781:/usr/local/go/src# go test ./math/
ok math (cached)
The content of the tests is not the only thing that Go is checking, it also checks the environment variables and the command-line arguments. Updating an environment variable or adding a flag will not use the cache:
go test ./math/ -v[...]
=== RUN ExampleRoundToEven
--- PASS: ExampleRoundToEven (0.00s)
PASS
ok math 0.007s
Running it again will now use the cache. The cache is a hash of the content, environment variables, and command-line arguments. Once calculated, it is dumped to the folder $GOCACHE
, that points by default on Unix to $XDG_CACHE_HOME
or $HOME/.cache
. Cleaning…