Documentation
¶
Index ¶
- func Append(file string, text string) error
- func CSVToLines(filename string) ([][]string, error)
- func Create(filePath string, fileContents []byte) error
- func CreateDirectory(path string) error
- func CreateEmpty(name string) bool
- func Delete(file string) error
- func Exists(fileLoc string) bool
- func Find(fileName string, dirs []string) (string, error)
- func FindStr(path string, searchStr string) (bool, error)
- func ListR(path string) ([]string, error)
- func ToSlice(fileName string) ([]string, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Append ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
Find searches for a specified filename in a set of directories. Returns the file path if found, or an error if the file cannot be found.
Parameters:
fileName: The name of the file to find. dirs: A slice of strings representing the directories to search in.
Returns:
string: The file path 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"} filePath, err := Find(fileName, dirs)
if err != nil {
log.Fatalf("failed to find file: %v", err)
}
fmt.Printf("File found at: %s\n", filePath)
func FindStr ¶
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 ¶
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 ¶
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.