chapter six

6 Testing, debugging, and benchmarking

 

This chapter covers

  • Utilizing Go’s robust built-in tooling to help create well-formatted and readable code with updated dependencies.
  • Using logging with best practices to create resilient and structured logs for your application, saved to disk or other destinations.
  • Creating unit tests using Go’s testing framework and building strategies for designing test suites.
  • Analyzing your code’s performance, and finding bottlenecks and problems.

Go provides several tools out of the box that help keep your code clean, performant, and up to date. As you by now have experienced, the go tool lets you build and run your packages, but it also provides some incredibly useful functionality that can help make working code rock solid and production ready. To get to this point, code typically should have good testing, both for performance and functionality (unit testing). We can use all of Go’s tooling to do this, without the need for third-party software.

In this chapter we’ll dig into linting our code, keeping imports up to date, best practice logging strategies, and testing and benchmarking our code. We’ll also look at features some common Integrated Development Environments, or IDEs, can provide to push us even further toward battle-tested apps and reduce errors in production.

6.1 Keeping your code and projects clean

6.1.1 Keeping code well-formatted

6.1.2 Catch common errors with go vet

6.1.3 Keeping dependencies updated

6.2 Logging

6.2.1 Logging data to different outputs

6.2.2 Going deeper with structured logging

6.2.3 Accessing and capturing stack traces

6.3 Unit testing in Go

6.3.1 Creating a test suit with table-driven tests

6.3.2 Fuzzing test input

6.3.3 Annotating tests with names

6.3.4 Checking test coverage with go cover

6.3.5 Wait, where is my debugger?

6.4 Benchmarking and performance tuning

6.5 Summary