files

package
v0.0.0-...-1ad5891 Latest Latest
Warning

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

Go to latest
Published: Jan 31, 2022 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package fileutil provides functions for doing things with files, like reading them line by line, etc

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Exists

func Exists(path string) (bool, error)

Exists returns whether or not the given file or directory exists

func Pwd

func Pwd() (string, error)

Pwd returns the present working directory

func ReadLinesChannel

func ReadLinesChannel(filePath string) (<-chan string, error)

ReadLinesChannel reads a text file line by line into a channel.

c, err := fileutil.ReadLinesChannel(fileName)
if err != nil {
   log.Fatalf("readLines: %s\n", err)
}
for line := range c {
   fmt.Printf("  Line: %s\n", line)
}

nil is returned (with the error) if there is an error opening the file

func ReadLinesSlice

func ReadLinesSlice(path string) ([]string, error)

ReadLinesSlice reads a text file line by line into a slice of strings. Not recommended for use with very large files due to the memory needed.

lines, err := fileutil.ReadLinesSlice(filePath)
if err != nil {
    log.Fatalf("readLines: %s\n", err)
}
for i, line := range lines {
    fmt.Printf("  Line: %d %s\n", i, line)
}

nil is returned if there is an error opening the file

func ReadPropertiesFile

func ReadPropertiesFile(fileName string) (map[string]string, error)

Reads name-value pairs from a properties file Property file has lines in the format of "name = value" (leading and trailing spaces are ignored)

func TempFileName

func TempFileName(prefix, suffix string) string

TempFileName generates a temporary filename for use in testing or whatever

func WriteLinesSlice

func WriteLinesSlice(lines []string, path string) error

WriteLinesSlice writes the given slice of lines to the given file.

Types

type SynchronizedFile

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

SynchronizedFile wraps an os.File pointer with a mutex

func NewSynchronizedFile

func NewSynchronizedFile(f *os.File) *SynchronizedFile

NewSynchronizedFile synchronizes writing to a writer

func (*SynchronizedFile) Close

func (sf *SynchronizedFile) Close() error

Close closes the file

func (*SynchronizedFile) WriteString

func (sf *SynchronizedFile) WriteString(text string) (int, error)

WriteString writes to the file

Jump to

Keyboard shortcuts

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