func randFile(dir string, prefix, suffix string) (fname string) {
	for i := 0; i < 1000; i++ {
		fname = filepath.Join(dir, prefix+nextRandom()+suffix)
		if _, err := os.Stat(fname); !os.IsNotExist(err) {
			continue
		}
		break
	}
	return fname
}
Parts of this function came from the Go standard library io/ioutil/tempfile.go
package tests
import (
	"os"
	"path/filepath"
	"strconv"
	"sync"
	"testing"
	"time"
)
var (
	rand uint32
	mu   sync.Mutex
)
func TempFile() string {
	return randFile(os.TempDir(), "", "")
}
func NamedTempFile(prefix, suffix string) string {
	return randFile(os.TempDir(), prefix, suffix)
}
func WithTempFile(test func(string, *testing.T)) func(*testing.T) {
	return func(t *testing.T) {
		test(TempFile(), t)
	}
}
func TempDir() string {
	dir := randFile(os.TempDir(), "", "")
	if err := os.Mkdir(dir, 0755); err != nil {
		return os.TempDir()
	}
	return dir
}
func MkTempDir(name string) string {
	dir := randFile(os.TempDir(), name, "")
	if err := os.Mkdir(dir, 0755); err != nil {
		panic("could not create temp directory " + dir)
	}
	return dir
}