Table Of Contents
The filesystem package provides an expressive abstraction for working with local files.
package main
import "github.com/reglue4go/filesystem"
func main() {
if filesystem.Exists("./app.yml") {
fmt.Printf("%v\n", "File found")
} else {
fmt.Printf("%v\n", "File missing")
}
}
The UserHomeDirectory method will return the path to operating system user home directory.
path := filesystem.UserHomeDirectory()
The UserConfigDirectory method will return the path to operating system user config directory.
path := filesystem.UserConfigDirectory()
The Type method returns the file type of a given file. The returned value is either file or dir.
value := filesystem.Type("./temp.log")
The Tempnam method creates a file with a unique filename with access permission set to 0600 in the specified directory. The second parameter is a prefix to the temporal name.
path := filesystem.Tempnam("./public/temp", "log")
The TempDirectory method will return the path to operating system temporal directory.
path := filesystem.TempDirectory()
The Size method will gets the file size of a given file.
size := filesystem.Size("./temp.log")
The ReplaceInFile method will replace a given string within a given file.
ok := filesystem.ReplaceInFile("long text", "article", "./temp.log")
The Replace method writes the contents of a file, replacing it atomically if it already exists.
ok := filesystem.Replace("./temp.log", "Some long text contents")
The RealpathSystem method expands all symbolic links and returns canonicalized absolute pathname based on the given path prifix. ~C~ prefix will expand from operating system user config directory. ~T~ prefix will expand from operating system temp directory. ~H~ prefix will expand from operating system user home directory.
log1 := filesystem.RealpathSystem("~C~/data/temp.log")
log2 := filesystem.RealpathSystem("~T~/data/temp.log")
log3 := filesystem.RealpathSystem("~H~/data/temp.log")
log4 := filesystem.RealpathSystem("./data/temp.log")
The Realpath method expands all symbolic links and returns canonicalized absolute pathname.
path := filesystem.Realpath("./temp.log")
The Put method may be used to write the contents of a file. If the put method is unable to write the file to disk, false will be returned.
success := filesystem.Put("./temp.log", "Some long text contents")
The Prepend method allows you to write to the beginning of a file:
success := filesystem.Prepend("./temp.log", "Prepended Text")
The Name method may be used to extract the file name from a file path.
name := filesystem.Name("./public/images/cat.jpg") // cat
The MoveDirectory method may be used to move an existing directory to a new location. You can overide any existing directory by providing true as a third argument.
success := filesystem.MoveDirectory("./from/temp", "./to/backup/temp")
success := filesystem.MoveDirectory("./from/temp", "./to/backup/temp", true)
The Move method may be used to rename or move an existing file to a new location.
success := filesystem.Move("./from/file.jpg", "./to/file.jpg")
The Missing method may be used to determine if a file or directory is missing.
ok := filesystem.Missing("./cat.jpg")
ok := filesystem.Missing("./backup")
The MimeType method will obtain the MIME type of a given file.
mime := filesystem.MimeType("./dog.png")
The MakeDirectory method will create the given directory, including any needed subdirectories.
success := filesystem.MakeDirectory("./backup/temp")
The Link method will create a symlink to the target file or directory.
success := filesystem.Link("./temp.log", "./backup/temp.log")
The Lines method gets the contents of a file one line at a time returning an array of lines
var list []string = filesystem.Lines("./temp.log")
The LastModified method gets the file’s last modification time.
modifiedAt := filesystem.LastModified("./backup")
The IsWritable method determines if the given path is writable.
ok := filesystem.IsWritable("./backup")
The IsReadable method determines if the given path is readable.
ok := filesystem.IsReadable("./backup")
The IsFile method determines if the given path is a file.
ok := filesystem.IsFile("./backup")
The IsEmptyDirectory method determines if the given path is a directory that does not contain any other files or directories.
ok := filesystem.IsEmptyDirectory("./backup")
The IsDirectory method determines if the given path is a directory.
ok := filesystem.IsDirectory("./temp.log")
The HasSameHash method determines if two files are the same by comparing their hashes.
ok := filesystem.HasSameHash("./temp.log", "./backup/temp.log")
The Hash method generates a hash value using the contents of a given file.
value := filesystem.Hash("./temp.log")
The GuessExtension method returns the file extension from the mime-type of a given file.
path := filesystem.GuessExtension("./public/images") // ""
path := filesystem.GuessExtension("./public/images/cat.jpg") // "jpg"
The Glob method returns a list of all path names matching a given pattern:
items := filesystem.Glob("*.jpg")
The Get method may be used to retrieve the contents of a file.
contents := filesystem.Get("./temp.log")
The Files method returns a list of all of the files in a given directory:
items := filesystem.Files("./public")
The Extension method returns the file extension of the given path.
path := filesystem.Extension("./public/images") // ""
path := filesystem.Extension("./public/images/cat.jpg") // "jpg"
The Exists method may be used to determine if a file exists on the disk.
success := filesystem.Exists("./temp")
The EnsureDirectoryExists method determines if a directory exists or creates the directory if it does not exists.
success := filesystem.EnsureDirectoryExists("./temp")
The Dirname method returns the parent directory from a path.
path := filesystem.Dirname("./public/images")
// "./public"
path := filesystem.Dirname("./public/images/cat.jpg")
// "./public/images"
The Directories method returns a list of all the directories within a given directory.
items := filesystem.Directories("./public")
The DeleteDirectory method may be used to remove a directory and all of its files.
success := filesystem.DeleteDirectory("./temp")
The DeleteDirectories method may be used to remove all of the directories within a given directory.
success := filesystem.DeleteDirectories("./temp")
The Delete method may be used to remove the file at a given path.
success := filesystem.Delete("./public/temp.log")
The CurrentExecutable method may be used to get the path of the executable that started the current process.
path := filesystem.CurrentExecutable()
The CurrentDirectory method may be used to get path for the current working directory.
path := filesystem.CurrentDirectory()
The CopyDirectory method may be used to copy a directory from one location to another.
success := filesystem.CopyDirectory("./from/public", "./to/backup")
The Copy method may be used to copy an existing file to a new location on the disk.
success := filesystem.Copy("./from/file.jpg", "./to/file.jpg")
The CleanDirectory method will empty the specified directory of all files and folders.
success := filesystem.CleanDirectory("./temp")
The Chmod method will get or set UNIX mode of a file or directory
success := filesystem.Chmod("./public/temp.log", 775)
The Basename method will return the trailing name component of the given path.
name := filesystem.Basename("./public/temp.log")
// temp.log
The Append method allows you to write to the end of a file:
success := filesystem.Append("./temp.log", "Appended Text")
The AllFiles method retrieves a list of all files within a given directory including all subdirectories:
items := filesystem.AllFiles("./public")
This package has been tested on the following platforms.
golang | ubuntu | macos | windows |
---|---|---|---|
1.18.x | ✓ | ✓ | ✓ |
1.20.x | ✓ | ✓ | ✓ |
1.22.x | ✓ | ✓ | ✓ |