file

package
v0.0.57 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2024 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// Separator for file system.
	// It here defines the separator as variable
	// to allow it modified by developer if necessary.
	Separator = string(filepath.Separator)

	// DefaultPermOpen is the default perm for file opening.
	DefaultPermOpen = os.FileMode(0666)

	// DefaultPermCopy is the default perm for file/folder copy.
	DefaultPermCopy = os.FileMode(0755)
)

Variables

This section is empty.

Functions

func Abs added in v0.0.33

func Abs(path string) string

Abs returns an absolute representation of path. If the path is not absolute it will be joined with the current working directory to turn it into an absolute path. The absolute path name for a given file is not guaranteed to be unique. Abs calls Clean on the result.

func Basename added in v0.0.33

func Basename(path string) string

Basename returns the last element of path, which contains file extension. Trailing path separators are removed before extracting the last element. If the path is empty, Base returns ".". If the path consists entirely of separators, Basename returns a single separator.

Example: Basename("/var/www/file.js") -> file.js Basename("file.js") -> file.js

func Chdir added in v0.0.33

func Chdir(dir string) (err error)

Chdir changes the current working directory to the named directory. If there is an error, it will be of type *PathError.

func CheckNotExist

func CheckNotExist(src string) bool

func CheckPermission

func CheckPermission(src string) bool

func Chmod added in v0.0.33

func Chmod(path string, mode os.FileMode) (err error)

Chmod is alias of os.Chmod. See os.Chmod.

func Create added in v0.0.33

func Create(path string) (*os.File, error)

Create creates a file with given `path` recursively. The parameter `path` is suggested to be absolute path.

func Dir added in v0.0.33

func Dir(path string) string

Dir returns all but the last element of path, typically the path's directory. After dropping the final element, Dir calls Clean on the path and trailing slashes are removed. If the `path` is empty, Dir returns ".". If the `path` is ".", Dir treats the path as current working directory. If the `path` consists entirely of separators, Dir returns a single separator. The returned path does not end in a separator unless it is the root directory.

Example: Dir("/var/www/file.js") -> "/var/www" Dir("file.js") -> "."

func DirNames added in v0.0.33

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

DirNames returns sub-file names of given directory `path`. Note that the returned names are NOT absolute paths.

func Exist added in v0.0.33

func Exist(src string) bool

func Exists added in v0.0.33

func Exists(path string) bool

Exists checks whether given `path` exist.

func Ext added in v0.0.33

func Ext(path string) string

Ext returns the file name extension used by path. The extension is the suffix beginning at the final dot in the final element of path; it is empty if there is no dot. Note: the result contains symbol '.'.

Example: Ext("main.go") => .go Ext("api.json") => .json

func ExtName added in v0.0.33

func ExtName(path string) string

ExtName is like function Ext, which returns the file name extension used by path, but the result does not contain symbol '.'.

Example: ExtName("main.go") => go ExtName("api.json") => json

func GetBytes added in v0.0.33

func GetBytes(path string) []byte

GetBytes returns the file content of `path` as []byte. It returns nil if it fails reading.

func GetContents added in v0.0.33

func GetContents(path string) string

GetContents returns the file content of `path` as string. It returns en empty string if it fails reading.

func GetExt

func GetExt(fileName string) string

func GetSize

func GetSize(f multipart.File) (int, error)

func Glob added in v0.0.33

func Glob(pattern string, onlyNames ...bool) ([]string, error)

Glob returns the names of all files matching pattern or nil if there is no matching file. The syntax of patterns is the same as in Match. The pattern may describe hierarchical names such as /usr/*/bin/ed (assuming the Separator is '/').

Glob ignores file system errors such as I/O errors reading directories. The only possible returned error is ErrBadPattern, when pattern is malformed.

func IsDir added in v0.0.33

func IsDir(path string) bool

IsDir checks whether given `path` a directory. Note that it returns false if the `path` does not exist.

func IsEmpty added in v0.0.33

func IsEmpty(path string) bool

IsEmpty checks whether the given `path` is empty. If `path` is a folder, it checks if there's any file under it. If `path` is a file, it checks if the file size is zero.

Note that it returns true if `path` does not exist.

func IsFile added in v0.0.33

func IsFile(path string) bool

IsFile checks whether given `path` a file, which means it's not a directory. Note that it returns false if the `path` does not exist.

func IsNotExistMkDir

func IsNotExistMkDir(src string) error

func IsReadable added in v0.0.33

func IsReadable(path string) bool

IsReadable checks whether given `path` is readable.

func IsWritable added in v0.0.33

func IsWritable(path string) bool

IsWritable checks whether given `path` is writable.

TODO improve performance; use golang.org/x/sys to cross-plat-form

func Join added in v0.0.33

func Join(paths ...string) string

Join joins string array paths with file separator of current system.

func MkDir

func MkDir(src string) error

func Mkdir added in v0.0.33

func Mkdir(path string) (err error)

Mkdir creates directories recursively with given `path`. The parameter `path` is suggested to be an absolute path instead of relative one.

func Move added in v0.0.33

func Move(src string, dst string) (err error)

Move renames (moves) `src` to `dst` path. If `dst` already exists and is not a directory, it'll be replaced.

func MustOpen

func MustOpen(fileName, filePath string) (*os.File, error)

func Name added in v0.0.33

func Name(path string) string

Name returns the last element of path without file extension.

Example: Name("/var/www/file.js") -> file Name("file.js") -> file

func Open

func Open(path string) (*os.File, error)

Open opens file/directory READONLY.

func OpenFile added in v0.0.33

func OpenFile(path string, flag int, perm os.FileMode) (*os.File, error)

OpenFile opens file/directory with custom `flag` and `perm`. The parameter `flag` is like: O_RDONLY, O_RDWR, O_RDWR|O_CREATE|O_TRUNC, etc.

func OpenWithFlag added in v0.0.33

func OpenWithFlag(path string, flag int) (*os.File, error)

OpenWithFlag opens file/directory with default perm and custom `flag`. The default `perm` is 0666. The parameter `flag` is like: O_RDONLY, O_RDWR, O_RDWR|O_CREATE|O_TRUNC, etc.

func OpenWithFlagPerm added in v0.0.33

func OpenWithFlagPerm(path string, flag int, perm os.FileMode) (*os.File, error)

OpenWithFlagPerm opens file/directory with custom `flag` and `perm`. The parameter `flag` is like: O_RDONLY, O_RDWR, O_RDWR|O_CREATE|O_TRUNC, etc. The parameter `perm` is like: 0600, 0666, 0777, etc.

func Pwd added in v0.0.33

func Pwd() string

Pwd returns absolute path of current working directory. Note that it returns an empty string if retrieving current working directory failed.

func RealPath added in v0.0.33

func RealPath(path string) string

RealPath converts the given `path` to its absolute path and checks if the file path exists. If the file does not exist, return an empty string.

func Remove added in v0.0.33

func Remove(path string) (err error)

Remove deletes all file/directory with `path` parameter. If parameter `path` is directory, it deletes it recursively.

It does nothing if given `path` does not exist or is empty.

func Rename added in v0.0.33

func Rename(src string, dst string) error

Rename is alias of Move. See Move.

func ScanDir added in v0.0.33

func ScanDir(path string, pattern string, recursive ...bool) ([]string, error)

ScanDir returns all sub-files with absolute paths of given `path`, It scans directory recursively if given parameter `recursive` is true.

The pattern parameter `pattern` supports multiple file name patterns, using the ',' symbol to separate multiple patterns.

func ScanDirFile added in v0.0.33

func ScanDirFile(path string, pattern string, recursive ...bool) ([]string, error)

ScanDirFile returns all sub-files with absolute paths of given `path`, It scans directory recursively if given parameter `recursive` is true.

The pattern parameter `pattern` supports multiple file name patterns, using the ',' symbol to separate multiple patterns.

Note that it returns only files, exclusive of directories.

func ScanDirFileFunc added in v0.0.33

func ScanDirFileFunc(path string, pattern string, recursive bool, handler func(path string) string) ([]string, error)

ScanDirFileFunc returns all sub-files with absolute paths of given `path`, It scans directory recursively if given parameter `recursive` is true.

The pattern parameter `pattern` supports multiple file name patterns, using the ',' symbol to separate multiple patterns.

The parameter `recursive` specifies whether scanning the `path` recursively, which means it scans its sub-files and appends the file paths to result array if the sub-file is also a folder. It is false in default.

The parameter `handler` specifies the callback function handling each sub-file path of the `path` and its sub-folders. It ignores the sub-file path if `handler` returns an empty string, or else it appends the sub-file path to result slice.

Note that the parameter `path` for `handler` is not a directory but a file. It returns only files, exclusive of directories.

func ScanDirFunc added in v0.0.33

func ScanDirFunc(path string, pattern string, recursive bool, handler func(path string) string) ([]string, error)

ScanDirFunc returns all sub-files with absolute paths of given `path`, It scans directory recursively if given parameter `recursive` is true.

The pattern parameter `pattern` supports multiple file name patterns, using the ',' symbol to separate multiple patterns.

The parameter `recursive` specifies whether scanning the `path` recursively, which means it scans its sub-files and appends the files path to result array if the sub-file is also a folder. It is false in default.

The parameter `handler` specifies the callback function handling each sub-file path of the `path` and its sub-folders. It ignores the sub-file path if `handler` returns an empty string, or else it appends the sub-file path to result slice.

func Stat added in v0.0.33

func Stat(path string) (os.FileInfo, error)

Stat returns a FileInfo describing the named file. If there is an error, it will be of type *PathError.

func Temp added in v0.0.33

func Temp(names ...string) string

Temp retrieves and returns the temporary directory of current system.

The optional parameter `names` specifies the sub-folders/sub-files, which will be joined with current system separator and returned with the path.

Types

This section is empty.

Jump to

Keyboard shortcuts

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