2 Idioms and testing
This chapter covers
- Exploring idiomatic principles with packages.
- Satisfying the standard library interfaces.
- Writing and running tests using the tools provided by Go.
- Using table-driven testing and subtests to improve maintainability.
- Writing example tests to generate runnable documentation.
Automated tests ensure the code works today and will continue to work in the future. Focusing on testing right away builds a solid mindset for the remainder of the book. We'll cover the testing fundamentals, while the next chapters will explore it in greater depth.
We'll explore how to write and test a simple URL parser package called url. A simple replica of the standard library's url package with the same name. Although simple, it shows us how to properly name packages and their items, handle errors, and implement interfaces from the standard library. We'll then test the package once we have its basic version in place.
After learning the fundamentals of testing in Go, we'll explore table-driven tests to reduce duplication. While table-driven testing is helpful, it has some pitfalls, which we'll address using subtests. Lastly, we'll learn how to generate documentation using example tests.
The next chapter dives into measuring test coverage, benchmarking, and optimization.