files

package
v1.2.5 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2021 License: GPL-3.0 Imports: 1 Imported by: 2

Documentation

Overview

A convenience wrapper for file checks and operations using paths.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Exists added in v1.0.0

func Exists(path string) bool

Returns whether the given path exists regardless of what mode the pointed to file has.

Example

Checking if temporary files and directories created are said to exist by the Exists function.

// We create a temporary file and a temporary directory to test the function.
tmpFile, _ := ioutil.TempFile("", "tmpFile")
tmpDir, _ := ioutil.TempDir("", "tmpDir")

// Checking tmpFile
fmt.Println("Checking file (before removal):", Exists(tmpFile.Name()))
// Removing the file should result in "false" from Exists
_ = os.Remove(tmpFile.Name())
fmt.Println("Checking file (after removal):", Exists(tmpFile.Name()))

// Checking tmpDir
fmt.Println("Checking directory (before removal):", Exists(tmpDir))
// Removing the file should result in "false" from Exists
_ = os.RemoveAll(tmpDir)
fmt.Println("Checking directory (after removal):", Exists(tmpDir))
Output:
Checking file (before removal): true
Checking file (after removal): false
Checking directory (before removal): true
Checking directory (after removal): false

func IsDir added in v1.0.0

func IsDir(path string) bool

Checks if the given path exists a directory.

Returns true if the path given exists and it points to a directory, false otherwise.

Example

Checking if a temporary directory made exists and comparing outputs to a temporary file.

// We create a temporary file and a temporary directory to test the function.
tmpFile, _ := ioutil.TempFile("", "tmpFile")
tmpDir, _ := ioutil.TempDir("", "tmpDir")

// Checking tmpDir
fmt.Println("Checking directory (before removal):", IsDir(tmpDir))
// Removing the directory should result in "false" from IsDir
_ = os.RemoveAll(tmpDir)
fmt.Println("Checking directory (after removal):", IsDir(tmpDir))

// Checking tmpFile (both should be false)
fmt.Println("Checking file (before removal):", IsDir(tmpFile.Name()))
_ = os.Remove(tmpFile.Name())
fmt.Println("Checking file (after removal):", IsDir(tmpFile.Name()))
Output:
Checking directory (before removal): true
Checking directory (after removal): false
Checking file (before removal): false
Checking file (after removal): false

func IsFile added in v1.0.0

func IsFile(path string) bool

Checks if the given path exists and whether the path points to a file.

Returns true if the path exists and the path points to a file, otherwise false.

Example

Checking if a temporary file made exists and comparing outputs to a temporary directory.

// We create a temporary file and a temporary directory to test the function.
tmpFile, _ := ioutil.TempFile("", "tmpFile")
tmpDir, _ := ioutil.TempDir("", "tmpDir")

// Checking tmpFile
fmt.Println("Checking file (before removal):", IsFile(tmpFile.Name()))
// Removing the file should result in "false" from IsFile
_ = os.Remove(tmpFile.Name())
fmt.Println("Checking file (after removal):", IsFile(tmpFile.Name()))

// Checking tmpDir (both should be false)
fmt.Println("Checking directory (before removal):", IsFile(tmpDir))
_ = os.RemoveAll(tmpDir)
fmt.Println("Checking directory (after removal):", IsFile(tmpDir))
Output:
Checking file (before removal): true
Checking file (after removal): false
Checking directory (before removal): false
Checking directory (after removal): false

Types

This section is empty.

Jump to

Keyboard shortcuts

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