tmpfile

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: May 19, 2021 License: MIT Imports: 2 Imported by: 2

README

Documentation Test Status

Cross Platform Temporary Files

This library attempts to bridge the gap between the what is provided in ioutil.TempFile and the best practice of ensuring temporary files are always deleted when the application exits.

The normal way to do this on a POSIX system is to use the behavior of unlink to immediately remove the directory entry for the temporary file. The OS then ensures that when all open file handles on the file are close that the file resources are removed. Unfortunately, despite Go having os.Remove this does not work on Windows because on Windows it is necessary to open the files with special flags (FILE_SHARE_DELETE, FILE_FLAG_DELETE_ON_CLOSE) to enable removing a file that is open (and ioutil does not do this).

Example usage:

package main

import "github.com/calebcase/tmpfile"

func main() {
	f, err := tmpfile.New("", "example-*")
	if err != nil {
		panic(err)
	}
	defer f.Close()

	f.WriteString("Example Data")
}

Documentation

Overview

Package tmpfile provides a cross platform facility for creating temporary files that are automatically cleaned up (even in the event of an unexpected process exit).

tmpfile provides support for at least Linux, OSX, and Windows. Generally any POSIX system that adheres to the semantics of unlink (https://pubs.opengroup.org/onlinepubs/9699919799/functions/unlink.html) should work. Special handling is provided for other platforms.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(dir, pattern string) (f *os.File, err error)

New creates a new temporary file in the directory dir using ioutil.TempFile and then unlinks the file with os.Remove to ensure the file is deleted when the calling process exists.

If dir is the empty string it will default to using os.TempDir() as the directory for storing the temporary files.

The target directory dir must already exist or an error will result. New does not create or remove the directory dir.

Example (Default)
// Use the system default temp directory (as returned by os.TempDir().
// This is equivalent to New(os.TempDir(), "example-").
f, err := New("", "example-")
if err != nil {
	panic(err)
}
defer f.Close()
Output:

Example (Dir)
// Use a local directory for the temporary files. This directory must
// already exist.
f, err := New("ephemeral", "example-")
if err != nil {
	panic(err)
}
defer f.Close()
Output:

Types

This section is empty.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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