Documentation ¶
Overview ¶
Package tmp provides methods and structs for working with temporary data
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var DefaultDirPerms = os.FileMode(0750)
DefaultDirPerms is default permissions for directories
View Source
var DefaultFilePerms = os.FileMode(0640)
DefaultFilePerms is default permissions for files
View Source
var Dir = os.TempDir()
Dir is path to temporary directory
View Source
var ErrNilTemp = fmt.Errorf("Temp struct is nil")
ErrNilTemp is returned if temp struct is nil
Functions ¶
This section is empty.
Types ¶
type Temp ¶
type Temp struct { Dir string DirPerms os.FileMode FilePerms os.FileMode // contains filtered or unexported fields }
Temp is basic temp struct
func NewTemp ¶
NewTemp creates new Temp structure
Example ¶
tmp, err := NewTemp() if err != nil { panic(err.Error()) } tmp.DirPerms = 0700 tmp.FilePerms = 0600
Output:
func (*Temp) Clean ¶
func (t *Temp) Clean()
Clean removes all temporary objects (files and directories)
Example ¶
tmp, err := NewTemp() if err != nil { panic(err.Error()) } tmp.MkDir() // All temporary data (directories and files) will be removed tmp.Clean()
Output:
func (*Temp) MkDir ¶
MkDir creates temporary directory
Example ¶
tmp, err := NewTemp() if err != nil { panic(err.Error()) } // Create a temporary file with an auto-generated name and suffix "myapp" // The suffix is optional and can be omitted. tmpDir, err := tmp.MkDir("myapp") if err != nil { panic(err.Error()) } err = os.WriteFile(tmpDir+"/test.txt", []byte("Test data\n"), 0644) if err != nil { panic(err.Error()) }
Output:
func (*Temp) MkFile ¶
MkFile creates temporary file
Example ¶
tmp, err := NewTemp() if err != nil { panic(err.Error()) } // Create a temporary file with an auto-generated name and suffix ".txt" // The suffix is optional and can be omitted. fd, filename, err := tmp.MkFile(".txt") if err != nil { panic(err.Error()) } defer fd.Close() fmt.Fprint(fd, "Test data\n") fmt.Printf("Test data written to %s\n", filename)
Output:
func (*Temp) MkName ¶
MkName returns name for temporary object (file or directory)
Example ¶
tmp, err := NewTemp() if err != nil { panic(err.Error()) } // Create a temporary file with an auto-generated name and suffix ".txt" // The suffix is optional and can be omitted. filename := tmp.MkName(".txt") if filename == "" { panic("Can't create name for temporary file") } err = os.WriteFile(filename, []byte("Test data\n"), 0644) if err != nil { panic(err.Error()) }
Output:
Click to show internal directories.
Click to hide internal directories.