Archive · Week 11 · Mar 10 – 16, 2025from 2 items
The Go team accepted a proposal that expands the testing API and announced a new set of file‑access helpers that guard against path traversal. Both items are slated for Go 1.24 and will affect how tests are written and how applications open files.
Worth knowingstdlib
testing: Add T.Output() etc
- What changed
- The proposal adds a new method
Output()totesting.TBthat returns anio.Writer. The writer writes to the same stream asTB.Log, is internally line buffered, and does not add source locations or newlines. After a test or subtest finishes, callingOutputorWritewill panic. - Production impact
- The source does not say.
- Try it
- Create a test that calls
t.Output()and writes a line, then run the test to see the output appear with the test’s log formatting. - Source
- github.com/golang/go/issues/59928
Explain it
Understand it, then run it
Testing in Go often needs a way to print messages that are tied to the test’s output stream. The testing package already provides t.Log and t.Logf, but those helpers add source‑location information and newlines automatically. A new method, Output, was proposed for the testing.TB interface. It returns an io.Writer that writes to the same stream as t.Log, but it does not add caller information or newlines. The writer is line‑buffered and flushes when the test ends. After a test finishes, calling Output or writing to it panics.
Exercise
Write a small program that opens a file named data.txt in the current directory using the new traversal‑resistant API and prints its contents.
The 60-second version
This week the Go team added a new helper to the testing package that lets test writers send output directly to the test log stream. The method, called `Output`, returns an `io.Writer` that buffers lines and flushes them when the test ends, but it can no longer be used after the test finishes. The change is intended to make it easier to capture structured logs in tests. In the same release, the standard library introduced traversal‑resistant file APIs. These new helpers enforce that file paths are resolved relative to a specified root directory, helping developers avoid accidental path traversal vulnerabilities. Both updates are part of the upcoming Go 1.24 release and aim to improve safety and testability in Go applications.
Written by gpt-oss-20b · claims checked against the sources · archive, not individually reviewed