github.com/Liam-Williams/i18n4go@v0.2.7-0.20201028180611-670cbaceaa6b/test_fixtures/extract_strings/f_option/input_files/no_strings.go (about) 1 package app 2 3 import ( 4 "io/ioutil" 5 "os" 6 "path/filepath" 7 "strconv" 8 "sync" 9 "time" 10 ) 11 12 func TempDir(namePrefix string, cb func(tmpDir string, err error)) { 13 tmpDir, err := ioutil.TempDir("", namePrefix) 14 15 defer func() { 16 os.RemoveAll(tmpDir) 17 }() 18 19 cb(tmpDir, err) 20 } 21 22 func TempFile(namePrefix string, cb func(tmpFile *os.File, err error)) { 23 tmpFile, err := ioutil.TempFile("", namePrefix) 24 25 defer func() { 26 tmpFile.Close() 27 os.Remove(tmpFile.Name()) 28 }() 29 30 cb(tmpFile, err) 31 } 32 33 // TempPath generates a random file path in tmp, but does 34 // NOT create the actual directory 35 func TempPath(namePrefix string) string { 36 return filepath.Join(os.TempDir(), namePrefix, nextSuffix()) 37 } 38 39 // copied from http://golang.org/src/pkg/io/ioutil/tempfile.go 40 // Random number state. 41 // We generate random temporary file names so that there's a good 42 // chance the file doesn't exist yet - keeps the number of tries in 43 // TempFile to a minimum. 44 var rand uint32 45 var randmu sync.Mutex 46 47 func reseed() uint32 { 48 return uint32(time.Now().UnixNano() + int64(os.Getpid())) 49 } 50 51 func nextSuffix() string { 52 randmu.Lock() 53 r := rand 54 if r == 0 { 55 r = reseed() 56 } 57 r = r*1664525 + 1013904223 // constants from Numerical Recipes 58 rand = r 59 randmu.Unlock() 60 return strconv.Itoa(int(1e9 + r%1e9))[1:] 61 }