Canonical guidance

Use when

Avoid

Preferred pattern

func mustTempFile(t *testing.T, body string) string {
	t.Helper()
	f, err := os.CreateTemp(t.TempDir(), "*.txt")
	if err != nil {
		t.Fatal(err)
	}
	if _, err := f.WriteString(body); err != nil {
		t.Fatal(err)
	}
	return f.Name()
}

Anti-pattern

Explanation: This anti-pattern is tempting because reuse feels clean, but tests lose readability and local debugging signal.

Why

Related pages

Sources