files

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2021 License: MIT Imports: 3 Imported by: 0

README

files

Convenience file functions for Go.

What

This is a small collection of file functions to make development simpler.

  • NewCloser() creates a deferrable Closer with no return value, which holds a list of files to close. Any errors from each close operation will be printed to stderr with a timestamp.
  • LoadJSON() is a convenience function to load JSON from a file.
  • SaveJSON() is the saving counterpart to LoadJSON() which uses a Closer for consistent error output.
  • Exosts() tests if a file or directory exists.
  • DirExists() tests if a directory exists, and returns false if the path is a file.
  • FileExists() tests if a file exists, and returns false if the path is a directory.
  • WriteFile() writes a file to disk, logging both open and close errors to stderr with a timestamp. There is no return value.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DirExists

func DirExists(path string) bool

DirExists checks for the existence of a directory. It will return false if a file with that name exists.

func Exists

func Exists(path string) bool

Exists checks for the existence of a file or directory.

func FileExists

func FileExists(path string) bool

FileExists checks for the existence of a file. It will return false if a directory with that name exists.

func LoadJSON

func LoadJSON(fn string, out interface{}) error

LoadJSON and unmarshal structure.

func SaveJSON

func SaveJSON(path string, data interface{}) error

SaveJSON after marshalling neatly, logging any file errors to stderr.

func WriteFile

func WriteFile(path string, data []byte)

WriteFile saves a file with private permissions, logging all errors to stderr with a timestamp.

Types

type Closer

type Closer struct {
	// contains filtered or unexported fields
}

Closer holds open files to close them all in sequence, logging any errors.

func NewCloser

func NewCloser(files ...*os.File) *Closer

NewCloser returns a pointer to a closer.

func (*Closer) AddFile

func (c *Closer) AddFile(f *os.File) *Closer

AddFile adds a file pointer to the closer's list.

func (*Closer) Close

func (c *Closer) Close()

Close and remove all files in the list, and log any errors from the Close() call with a timestamp.

Jump to

Keyboard shortcuts

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