6 Formatting, testing, debugging, and benchmarking

 

This chapter covers

  • Creating well-formatted and readable code with updated dependencies
  • Making resilient and structured logs for your application
  • Creating unit tests 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 have experienced by now, 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, following best-practice logging strategies, and testing and benchmarking our code. We’ll also look at features some common integrated development environments (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 Catching 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

Summary