tmp

package
v12.73.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 26, 2023 License: Apache-2.0 Imports: 7 Imported by: 1

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

func NewTemp(dir ...string) (*Temp, error)

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

func (t *Temp) MkDir(nameSuffix ...string) (string, error)

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

func (t *Temp) MkFile(nameSuffix ...string) (*os.File, string, error)

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

func (t *Temp) MkName(nameSuffix ...string) string

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:

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL