tempura

package module
v0.0.0-...-1e4f579 Latest Latest
Warning

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

Go to latest
Published: May 28, 2014 License: MIT Imports: 2 Imported by: 0

README

tempura

Temporary files creation and manipulation helpers for Go

Documentation

Overview

Package tempura provides temporary files creation and manipulation helpers for the purposes of enhancing tests involving files creation.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Create

func Create(dir, prefix string, data []byte) (path string, err error)

Create a new temporary file in the directory dir with a name beginning with prefix, writes the provided data into it and returns it's path.

If dir is the empty string, TempFile uses the default directory for temporary files (see os.TempDir). Multiple programs calling TempFile simultaneously will not choose the same file. It is the caller's responsibility to remove the file when no longer needed.

Types

type TempFile

type TempFile struct {
	*os.File
	// contains filtered or unexported fields
}

TempFile represents an open temporary file descriptor

func FromBytes

func FromBytes(dir, prefix string, data []byte) (*TempFile, error)

FromBytes creates a new temporary file in the directory dir with a name beginning with prefix, opens the file for reading and writing, writes the provided data into it and returns seeks the underlying file object to 0.

If dir is the empty string, TempFile uses the default directory for temporary files (see os.TempDir). Multiple programs calling TempFile simultaneously will not choose the same file. The caller can use f.Name() to find the pathname of the file. It is the caller's responsibility to remove the file when no longer needed.

Example
var tmp *TempFile
var err error

// Creates a temporary file in /tmp dir, prefixed with "test_"
// and containing three bytes "a", "b", "c"
tmp, err = FromBytes("/tmp", "test_", []byte{'a', 'b', 'c'})
if err != nil {
	// Handle err
}
defer tmp.Close()
defer os.Remove(tmp.Name())

// The tmp file descriptor is seeked to 0 and ready to be read/written
data, err := tmp.Read(make([]byte, 3))
if err != nil {
	// Handle error
}

fmt.Println(string(data))
Output:

"abc"

Jump to

Keyboard shortcuts

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