fsutil

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Aug 23, 2025 License: MIT Imports: 8 Imported by: 2 modules, 4 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

fsutil

Go Reference Go Report Card CodeFactor Actions Status codecov

Package fsutil provides a collection of utility functions that extend the capabilities of the Go standard filesystem package. It offers a variety of tools for working with files and directories, making it easier to perform common filesystem operations.

Installation

go get github.com/shelepuginivan/fsutil

Documentation

Is available on pkg.go.dev.

Details about IsBinary, IsBinarySlow and IsBinaryFast

[!NOTE] Available since v0.2.0.

Each IsBinary, IsBinarySlow and IsBinaryFast report whether data is binary, i.e. a non human-readable text, such as image, audio or video stream.

  • IsBinarySlow has the best accuracy and the worst speed, since it checks the entire byte sequence.
  • IsBinaryFast has the worst accuracy and the best speed since it checks only first line of the sequence.
  • IsBinary is in the middle — it checks the first and every 2^N line (i.e. 0, 1, 2, 4, 8, 16, etc.), providing a good accuracy while keeping good performance.

The table below shows the accuracy of these functions, determined by testing against random byte sequences:

Function Minimal accuracy Actual accuracy
IsBinary 990/1000 (99.0%) 1000/1000 (100.0%)
IsBinarySlow 1000/1000 (100.0%) 1000/1000 (100.0%)
IsBinaryFast 950/1000 (95.0%) 991/1000 (99.1%)

Documentation

Overview

Package fsutil provides a collection of utility functions that extend the capabilities of the Go standard filesystem (fs) package. It offers a variety of tools for working with files and directories, making it easier to perform common filesystem operations.

This package is designed to enhance the functionality of the standard library by providing additional features and abstractions. It is particularly useful for developers looking to simplify their filesystem interactions in Go applications.

Index

Constants

View Source
const (
	CR = "\r" // Caret return.
	LF = "\n" // Line break.
)
View Source
const NewLine = LF // Platform-specific newline character.

Variables

This section is empty.

Functions

func AssertDirExists

func AssertDirExists(path string)

AssertDirExists panics if path does not exist or is not a directory.

func AssertFileExists

func AssertFileExists(path string)

AssertFileExists panics if path does not exist or is not a file.

func AssertPathExists

func AssertPathExists(path string)

AssertPathExists panics if path does not exist.

func CopyFile

func CopyFile(src, dst string) error

CopyFile copies file src to file dst. It does not support recursive copying.

func CreateAll

func CreateAll(file string) (*os.File, error)

CreateAll creates file and all its parent directories. If the file exists, it is truncated.

func DirExists

func DirExists(path string) bool

DirExists reports whether path exists and is a directory.

func FileExists

func FileExists(path string) bool

FileExists reports whether path exists and is a file.

func FirstExistingDir added in v0.4.0

func FirstExistingDir(paths ...string) (string, bool)

FirstExistingDir returns the first path that exists and is a directory. The second value returned reports whether at least one path exists.

func FirstExistingFile added in v0.4.0

func FirstExistingFile(paths ...string) (string, bool)

FirstExistingFile returns the first path that exists and is a regular file. The second value returned reports whether at least one path exists.

func FirstExistingPath added in v0.4.0

func FirstExistingPath(paths ...string) (string, bool)

FirstExistingPath returns the first path that exists. The second value returned reports whether at least one path exists.

func IsBinary added in v0.2.0

func IsBinary(data []byte) bool

IsBinary reports whether data is binary (non human-readable text). It checks the first and every 2^n line of the provided byte sequence (i.e. 0, 1, 2, 4, 8, ...), hence IsBinary may fail in rare cases.

If data is an empty slice or nil, false is returned.

For the most accurate determination, IsBinarySlow is recommended to be used. For the fastest determination, IsBinaryFast should be used.

func IsBinaryFast added in v0.2.0

func IsBinaryFast(data []byte) bool

IsBinaryFast reports whether data is binary (non human-readable text). It checks only first line of the file, hence the determination performance is the highest at a cost of accuracy.

If data is an empty slice or nil, false is returned.

For the most cases, IsBinary is recommended to be used. For the most accurate determination, IsBinarySlow is recommended to be used.

func IsBinarySlow added in v0.2.0

func IsBinarySlow(data []byte) bool

IsBinarySlow reports whether data is binary (non human-readable text). It checks the entire file, hence the determination accuracy is the highest at a cost of performance.

If data is an empty slice or nil, false is returned.

For the most cases, IsBinary is recommended to be used. For the fastest determination, IsBinaryFast is recommended be used.

func Must

func Must[T any](v T, err error) T

Must is a helper that wraps a call to a function returning an error and panics if the error is not nil. It is intended for use in variable initialization.

func PathExists

func PathExists(path string) bool

PathExists reports whether path exists.

func ReadLines

func ReadLines(path string) (lines []string, err error)

ReadLines reads a file and returns its content as a slice of strings.

func ReadString

func ReadString(path string) (string, error)

ReadString reads path and returns its content as string.

func RemoveAllIfExists

func RemoveAllIfExists(path string) error

RemoveAllIfExists is like os.RemoveAll except that it returns an error if the path does not exist.

func ReplaceForbiddenChars added in v0.3.0

func ReplaceForbiddenChars(name string) string

ReplaceForbiddenChars replaces characters forbidden in some filesystems with dash (-).

Characters replaced are:

  • < (less than)
  • > (greater than)
  • : (colon)
  • " (double quote)
  • / (slash)
  • \ (backslash)
  • | (pipe)
  • ? (question mark)
  • * (asterisk)

In addition, all leading and trailing whitespace characters are trimmed.

func ReplaceForbiddenCharsWith added in v0.3.0

func ReplaceForbiddenCharsWith(name string, newchar string) string

ReplaceForbiddenCharsWith replaces characters forbidden in some filesystems with the specified character.

Characters replaced are:

  • < (less than)
  • > (greater than)
  • : (colon)
  • " (double quote)
  • / (slash)
  • \ (backslash)
  • | (pipe)
  • ? (question mark)
  • * (asterisk)

In addition, all leading and trailing whitespace characters are trimmed.

func WriteAll

func WriteAll(path string, data []byte, perm ...fs.FileMode) error

WriteAll writes data to path, creating all necessary parent directories. If parent directories already exist, WriteAll does nothing to them. If file already exists, it is overwritten with the given data.

Permission perm is optional and defaults to os.ModePerm.

func WriteLines

func WriteLines(path string, content []string, perm ...fs.FileMode) error

WriteLines writes a slice of string as lines to a file.

WriteLines uses platform-specific linebreak character (LF on Unix, CRLF of Windows). Use WriteLinesWithNewlineChar if specific newline character is required.

Permission perm is optional and defaults to os.ModePerm.

func WriteLinesWithNewlineChar

func WriteLinesWithNewlineChar(path string, content []string, newline string, perm ...fs.FileMode) error

WriteLinesWithNewlineChar is like WriteLines, but allows to set newline character used.

func WriteString

func WriteString(path, content string, perm ...fs.FileMode) error

WriteString writes a string content to a file.

Permission perm is optional and defaults to os.ModePerm.

Types

This section is empty.

Jump to

Keyboard shortcuts

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