file

package
v2.0.3 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2023 License: MIT Imports: 8 Imported by: 0

README

goutils/v2/file

The file package is a part of goutils library.

It provides utility functions for file manipulation in Go.


Functions

Append
func Append(file string, text string) error

Appends an input text string to the end of the input file.

CreateEmpty
func CreateEmpty(name string) bool

Creates an empty file based on the name input. It returns true if the file was created, otherwise it returns false.

Create
func Create(filePath string, fileContents []byte) error

Creates a file at the input filePath with the specified fileContents.

CreateDirectory
func CreateDirectory(path string) error

Creates a directory at the input path. If any part of the input path doesn't exist, create it. Return an error if the path already exists.

CSVToLines
func CSVToLines(filename string) ([][]string, error)

Reads the contents of the specified CSV file and returns its contents as a two-dimensional string slice.

Delete
func Delete(file string) error

Deletes the input file.

Exists
func Exists(fileLoc string) bool

Returns true if a file specified with fileLoc exists. If the file does not exist, it returns false.

ToSlice
func ToSlice(fileName string) ([]string, error)

Reads an input file into a slice, removes blank strings, and returns it.

Find
func Find(fileName string, dirs []string) (string, error)

Looks for an input filename in the specified set of dirs. The filepath is returned if the filename is found.

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

Lists the files found recursively from the input path.

FindStr
func FindStr(path string, searchStr string) (bool, error)

Searches for input searchStr in the input filepath.


Installation

To use the goutils/v2/file package, you need to install it via go get:

go get github.com/l50/goutils/v2/file

Usage

After installation, you can import it in your project:

import "github.com/l50/goutils/v2/file"

Tests

To run the tests for the goutils/v2/file package, navigate to your $GOPATH/src/github.com/l50/goutils/v2/file directory and run go test:

go test -v

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.


License

This project is licensed under the MIT License - see the LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Append

func Append(file string, text string) error

Append appends an input text string to the end of the specified file. If the file does not exist, it will be created.

Parameters:

file: A string representing the path to the file. text: A string that will be appended to the end of the file.

Returns:

error: An error if the file cannot be opened or the string cannot be written to the file.

Example:

filePath := "/path/to/your/file" text := "text to be appended" err := Append(filePath, text)

if err != nil {
  log.Fatalf("failed to append text to file: %v", err)
}

func CSVToLines

func CSVToLines(filename string) ([][]string, error)

CSVToLines reads the contents of a CSV file and returns it as a two-dimensional string slice, where each element in the outer slice represents a row in the CSV file, and each element in the inner slice represents a value in that row. The first row of the CSV file, which is assumed to contain column headers, is skipped.

Parameters:

filename: A string representing the path to the CSV file.

Returns:

[][]string: A two-dimensional slice of strings representing the rows and values of the CSV file. error: An error if the file cannot be read or parsed.

Example:

csvFilePath := "/path/to/your/csv/file" records, err := CSVToLines(csvFilePath)

if err != nil {
  log.Fatalf("failed to read CSV file: %v", err)
}

for _, row := range records {
  fmt.Println(row)
}

func Create

func Create(filePath string, fileContents []byte) error

Create creates a file at the specified path with the provided content. If the file does not exist, it will be created.

Parameters:

filePath: A string representing the path to the file. fileContents: A byte slice containing the content to be written to the file.

Returns:

error: An error if the file cannot be created or the content cannot be written to the file.

Example:

filePath := "/path/to/your/file" content := []byte("content to be written") err := Create(filePath, content)

if err != nil {
  log.Fatalf("failed to create file: %v", err)
}

func CreateDirectory

func CreateDirectory(path string) error

CreateDirectory creates a directory at the specified path. If the directory already exists, it returns an error.

Parameters:

path: A string representing the path to the directory.

Returns:

error: An error if the directory cannot be created or if it already exists.

Example:

dirPath := "/path/to/your/directory" err := CreateDirectory(dirPath)

if err != nil {
  log.Fatalf("failed to create directory: %v", err)
}

func CreateEmpty

func CreateEmpty(name string) bool

CreateEmpty creates an empty file at the specified path. If a file with the same name already exists, it will be overwritten.

Parameters:

name: A string representing the path to the file.

Returns:

bool: Returns true if the file was created successfully, otherwise false.

Example:

filePath := "/path/to/your/file" success := CreateEmpty(filePath)

if !success {
  log.Fatalf("failed to create empty file")
}

func Delete

func Delete(file string) error

Delete deletes the specified file.

Parameters:

file: A string representing the path to the file.

Returns:

error: An error if the file cannot be deleted.

Example:

filePath := "/path/to/your/file" err := Delete(filePath)

if err != nil {
  log.Fatalf("failed to delete file: %v", err)
}

func Exists

func Exists(fileLoc string) bool

Exists checks whether a file at the specified path exists.

Parameters:

fileLoc: A string representing the path to the file.

Returns:

bool: Returns true if the file exists, otherwise false.

Example:

filePath := "/path/to/your/file" exists := Exists(filePath)

if !exists {
  log.Fatalf("file does not exist")
}

func Find

func Find(fileName string, dirs []string) ([]string, error)

Find searches for a specified filename in a set of directories and returns all matches found as a slice of file paths. If no matches are found, it returns an error.

Parameters:

fileName: The name of the file to find. dirs: A slice of strings representing the directories to search in.

Returns:

[]string: A slice of file paths if the file is found. error: An error if the file cannot be found.

Example:

fileName := "file_to_find.txt" dirs := []string{"/path/to/first/directory", "/path/to/second/directory"} filePaths, err := Find(fileName, dirs)

if err != nil {
  log.Fatalf("failed to find file: %v", err)
}

for _, filePath := range filePaths {
    fmt.Printf("File found at: %s\n", filePath)
}

func FindStr

func FindStr(path string, searchStr string) (bool, error)

FindStr searches for a string in a specified file.

Parameters:

path: A string representing the path to the file. searchStr: The string to search for in the file.

Returns:

bool: Returns true if the string is found, otherwise false. error: An error if the file cannot be read.

Example:

filePath := "/path/to/your/file" searchStr := "text to find" found, err := FindStr(filePath, searchStr)

if err != nil {
  log.Fatalf("failed to search file: %v", err)
}

if found {
  fmt.Printf("'%s' found in file\n", searchStr)
}

func ListR

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

ListR lists all files in a directory and its subdirectories.

Parameters:

path: A string representing the path to the directory.

Returns:

[]string: A slice of strings representing the paths of the files found. error: An error if the files cannot be listed.

Example:

dirPath := "/path/to/your/directory" files, err := ListR(dirPath)

if err != nil {
  log.Fatalf("failed to list files: %v", err)
}

for _, file := range files {
  fmt.Println(file)
}

func ToSlice

func ToSlice(fileName string) ([]string, error)

ToSlice reads a file and returns its content as a slice of strings, where each element represents a line in the file. Blank lines are omitted.

Parameters:

fileName: A string representing the path to the file.

Returns:

[]string: A slice of strings where each element represents a line in the file. error: An error if the file cannot be read.

Example:

filePath := "/path/to/your/file" lines, err := ToSlice(filePath)

if err != nil {
  log.Fatalf("failed to read file: %v", err)
}

for _, line := range lines {
  fmt.Println(line)
}

Types

This section is empty.

Jump to

Keyboard shortcuts

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