Describe the solution you'd like
I found a new pattern you could support
file, err := os.CreateTemp("", "testfile")
if err != nil {
t.Fail("creating temporary test file")
}
If you take a look at os.CreateTemp code
https://cs.opensource.google/go/go/+/go1.22.4:src/os/tempfile.go;l=33
You will see os.CreateTemp creates a temporary folder is the dir is an empty string
The very same way os.MkdirTemp
https://cs.opensource.google/go/go/+/refs/tags/go1.22.4:src/os/tempfile.go;l=85
So your code should suggest this:
file, err := os.CreateTemp(t.TempDir(), "testfile")
if err != nil {
t.Fail("creating temporary test file")
}
Please have look at https://pkg.go.dev/testing#T.TempDir
and its implementation https://cs.opensource.google/go/go/+/refs/tags/go1.22.4:src/testing/testing.go;l=1229
No need for cleaning up the file, then because the whole temp folder is nuked by the cleanup added via t.TempDir