Chapter 5. Debugging and testing


This chapter covers

  • Capturing debugging information
  • Using a logger to log error and debugging information
  • Working with stack traces
  • Writing unit tests
  • Creating acceptance tests
  • Detecting race conditions
  • Running performance tests

One of the advantages of working with a modern language is tooling. Over the years, developers have created fantastic tools that streamline the development process. Go is designed as a language for system developers, and it’s loaded with tools designed to make your job easier. This chapter focuses on those tools and strategies for building resilient software. We talk about logging, debugging, and different sorts of testing.

In the preceding chapter, we talked about errors and panics. It seems fitting that we begin this chapter with techniques for finding the sorts of bugs that lead to unexpected errors and panics. We’ll begin with debugging.

5.1. Locating bugs

Sometimes you see a bug and know immediately what caused it. More often, you need to spend a short amount of time in the code hunting for the problem. And every once in a while, you hit those frustrating beasts that take hours or even days to track down.

That third category of bug usually warrants the use of special tools or tactics to track down the issue. This section covers some of those tools and tactics.

5.1.1. Wait, where is my debugger?

5.2. Logging

5.3. Accessing stack traces

5.4. Testing

5.5. Using performance tests and benchmarks

5.6. Summary