go test <package>

Stefan Alfbo - Feb 26 - - Dev Community

The testing story in Go is very cool. There are much things to uncover and learn when working with tests in Go. With the command go test you can use package as a filter when running the tests.

go test my_package
Enter fullscreen mode Exit fullscreen mode

You can read more about that command here, test packages.

One (of many) interesting thing with Go is when you install it on your machine, the source code for the standard library is included in the installation. This source code is valuable for reference, understanding how the standard library is implemented, and debugging purposes. It typically resides in the src directory within the Go installation path.

This enable us to run the tests for the standard packages also.

go test fmt
ok      fmt     0.120s

# or

go test net/http
ok      net/http        66.037s
Enter fullscreen mode Exit fullscreen mode

Nice, and you will be able to run this command in any folder! By using the verbose flag, -v, you will get a hint of what is being tested in each package, which is neat.

go test -v fmt
=== RUN   TestErrorf
--- PASS: TestErrorf (0.00s)
=== RUN   TestFmtInterface
--- PASS: TestFmtInterface (0.00s)
=== RUN   TestSprintf
--- PASS: TestSprintf (0.00s)
=== RUN   TestComplexFormatting
--- PASS: TestComplexFormatting (0.00s)
=== RUN   TestReorder
--- PASS: TestReorder (0.00s)
=== RUN   TestCountMallocs
    fmt_test.go:1451: skipping; GOMAXPROCS>1
--- SKIP: TestCountMallocs (0.00s)
=== RUN   TestFlagParser
--- PASS: TestFlagParser (0.00s)
...
...
=== RUN   ExampleStringer
--- PASS: ExampleStringer (0.00s)
Enter fullscreen mode Exit fullscreen mode

This could be a fun way to explore and understand how the standard packages are built in Go.

Happy exploring!

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .