Snapshot comparison checks software output against a stored reference. In Go tests, that reference is commonly a golden file: a fixture containing the expected contents, often kept in a testdata directory. section 11.1.1 section 11.6
A practical HTTP response comparison follows these steps:
- Record the handler response with httptest.NewRecorder. section 11.1.1
- Read the expected artifact, such as testdata/index.html, with os.ReadFile. section 11.1.1
- Compare the stored contents with rr.Body.String() and report read errors separately. section 11.1.1
- Treat exact whole-output equality cautiously: insignificant tabs and carriage returns can make a test fail. section 11.1.1
Checking a generated HTML page
A handler writes an HTML index page, while the test loads testdata/index.html and compares it with the recorded response. This automates detection of changes without placing the expected HTML inside the Go source file. section 11.1.1 section 11.6
