Source file src/io/ioutil/tempfile.go
1 // Copyright 2010 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package ioutil 6 7 import ( 8 "os" 9 ) 10 11 // TempFile creates a new temporary file in the directory dir, 12 // opens the file for reading and writing, and returns the resulting *os.File. 13 // The filename is generated by taking pattern and adding a random 14 // string to the end. If pattern includes a "*", the random string 15 // replaces the last "*". 16 // If dir is the empty string, TempFile uses the default directory 17 // for temporary files (see os.TempDir). 18 // Multiple programs calling TempFile simultaneously 19 // will not choose the same file. The caller can use f.Name() 20 // to find the pathname of the file. It is the caller's responsibility 21 // to remove the file when no longer needed. 22 // 23 // As of Go 1.17, this function simply calls os.CreateTemp. 24 func TempFile(dir, pattern string) (f *os.File, err error) { 25 return os.CreateTemp(dir, pattern) 26 } 27 28 // TempDir creates a new temporary directory in the directory dir. 29 // The directory name is generated by taking pattern and applying a 30 // random string to the end. If pattern includes a "*", the random string 31 // replaces the last "*". TempDir returns the name of the new directory. 32 // If dir is the empty string, TempDir uses the 33 // default directory for temporary files (see os.TempDir). 34 // Multiple programs calling TempDir simultaneously 35 // will not choose the same directory. It is the caller's responsibility 36 // to remove the directory when no longer needed. 37 // 38 // As of Go 1.17, this function simply calls os.MkdirTemp. 39 func TempDir(dir, pattern string) (name string, err error) { 40 return os.MkdirTemp(dir, pattern) 41 } 42