gf: github.com/gogf/gf/os/gfile Index | Files

package gfile

import "github.com/gogf/gf/os/gfile"

Package gfile provides easy-to-use operations for file system.

Index

Package Files

gfile.go gfile_cache.go gfile_contents.go gfile_copy.go gfile_home.go gfile_replace.go gfile_scan.go gfile_search.go gfile_size.go gfile_sort.go gfile_source.go gfile_time.go

Variables

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

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

    // DefaultPermCopy is the default perm for file/folder copy.
    DefaultPermCopy = os.FileMode(0777)
)
var (
    // Buffer size for reading file content.
    DefaultReadBuffer = 1024
)

func Abs Uses

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 Uses

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: /var/www/file.js -> file.js file.js -> file.js

func Chdir Uses

func Chdir(dir string) error

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

func Chmod Uses

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

See os.Chmod.

func Copy Uses

func Copy(src string, dst string) error

Copy file/directory from <src> to <dst>.

If <src> is file, it calls CopyFile to implements copy feature, or else it calls CopyDir.

func CopyDir Uses

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

CopyDir recursively copies a directory tree, attempting to preserve permissions.

Note that, the Source directory must exist and symlinks are ignored and skipped.

func CopyFile Uses

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

CopyFile copies the contents of the file named <src> to the file named by <dst>. The file will be created if it does not exist. If the destination file exists, all it's contents will be replaced by the contents of the source file. The file mode will be copied from the source and the copied data is synced/flushed to stable storage. Thanks: https://gist.github.com/r0l1/92462b38df26839a3ca324697c8cba04

func Create Uses

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

Create creates file with given <path> recursively. The parameter <path> is suggested to be absolute path.

func Dir Uses

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 consists entirely of separators, Dir returns a single separator. The returned path does not end in a separator unless it is the root directory.

func DirNames Uses

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 Exists Uses

func Exists(path string) bool

Exists checks whether given <path> exist.

func Ext Uses

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 '.'.

func ExtName Uses

func ExtName(path string) string

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

func FormatSize Uses

func FormatSize(raw int64) string

FormatSize formats size <raw> for more human readable.

func GetBytes Uses

func GetBytes(path string) []byte

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

func GetBytesByTwoOffsets Uses

func GetBytesByTwoOffsets(reader io.ReaderAt, start int64, end int64) []byte

GetBytesByTwoOffsets returns the binary content as []byte from <start> to <end>. Note: Returned value does not contain the character of the last position, which means it returns content range as [start, end).

func GetBytesByTwoOffsetsByPath Uses

func GetBytesByTwoOffsetsByPath(path string, start int64, end int64) []byte

GetBytesByTwoOffsetsByPath returns the binary content as []byte from <start> to <end>. Note: Returned value does not contain the character of the last position, which means it returns content range as [start, end). It opens file of <path> for reading with os.O_RDONLY flag and default perm.

func GetBytesTilChar Uses

func GetBytesTilChar(reader io.ReaderAt, char byte, start int64) ([]byte, int64)

GetBytesTilChar returns the contents of the file as []byte until the next specified byte <char> position.

Note: Returned value contains the character of the last position.

func GetBytesTilCharByPath Uses

func GetBytesTilCharByPath(path string, char byte, start int64) ([]byte, int64)

GetBytesTilCharByPath returns the contents of the file given by <path> as []byte until the next specified byte <char> position. It opens file of <path> for reading with os.O_RDONLY flag and default perm.

Note: Returned value contains the character of the last position.

func GetBytesWithCache Uses

func GetBytesWithCache(path string, duration ...time.Duration) []byte

GetBinContents returns []byte content of given file by <path> from cache. If there's no content in the cache, it will read it from disk file specified by <path>. The parameter <expire> specifies the caching time for this file content in seconds.

func GetContents Uses

func GetContents(path string) string

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

func GetContentsWithCache Uses

func GetContentsWithCache(path string, duration ...time.Duration) string

GetContents returns string content of given file by <path> from cache. If there's no content in the cache, it will read it from disk file specified by <path>. The parameter <expire> specifies the caching time for this file content in seconds.

func GetNextCharOffset Uses

func GetNextCharOffset(reader io.ReaderAt, char byte, start int64) int64

GetNextCharOffset returns the file offset for given <char> starting from <start>.

func GetNextCharOffsetByPath Uses

func GetNextCharOffsetByPath(path string, char byte, start int64) int64

GetNextCharOffsetByPath returns the file offset for given <char> starting from <start>. It opens file of <path> for reading with os.O_RDONLY flag and default perm.

func Glob Uses

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 Home Uses

func Home(names ...string) (string, error)

Home returns absolute path of current user's home directory. The optional parameter <names> specifies the its sub-folders/sub-files, which will be joined with current system separator and returned with the path.

func Info Uses

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

Alias of Stat. See Stat.

func IsDir Uses

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 Uses

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 Uses

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 IsReadable Uses

func IsReadable(path string) bool

IsReadable checks whether given <path> is readable.

func IsWritable Uses

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 Uses

func Join(paths ...string) string

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

func MTime Uses

func MTime(path string) time.Time

MTime returns the modification time of file given by <path> in second.

func MTimestamp Uses

func MTimestamp(path string) int64

MTimestamp returns the modification time of file given by <path> in second.

func MTimestampMilli Uses

func MTimestampMilli(path string) int64

MTimestampMilli returns the modification time of file given by <path> in millisecond.

func MainPkgPath Uses

func MainPkgPath() string

MainPkgPath returns absolute file path of package main, which contains the entrance function main.

It's only available in develop environment.

Note1: Only valid for source development environments, IE only valid for systems that generate this executable.

Note2: When the method is called for the first time, if it is in an asynchronous goroutine, the method may not get the main package path.

func Mkdir Uses

func Mkdir(path string) error

Mkdir creates directories recursively with given <path>. The parameter <path> is suggested to be absolute path.

func Move Uses

func Move(src string, dst string) error

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

func Name Uses

func Name(path string) string

Name returns the last element of path without file extension. Example: /var/www/file.js -> file file.js -> file

func Open Uses

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

Open opens file/directory READONLY.

func OpenFile Uses

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 Uses

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 Uses

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 PutBytes Uses

func PutBytes(path string, content []byte) error

PutBytes puts binary <content> to file of <path>. It creates file of <path> recursively if it does not exist.

func PutBytesAppend Uses

func PutBytesAppend(path string, content []byte) error

PutBytesAppend appends binary <content> to file of <path>. It creates file of <path> recursively if it does not exist.

func PutContents Uses

func PutContents(path string, content string) error

PutContents puts string <content> to file of <path>. It creates file of <path> recursively if it does not exist.

func PutContentsAppend Uses

func PutContentsAppend(path string, content string) error

PutContentsAppend appends string <content> to file of <path>. It creates file of <path> recursively if it does not exist.

func Pwd Uses

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 ReadByteLines Uses

func ReadByteLines(file string, callback func(bytes []byte)) error

ReadByteLines reads file content line by line, which is passed to the callback function <callback> as []byte. It matches each line of text, separated by chars '\r' or '\n', stripped any trailing end-of-line marker.

Note that the parameter passed to callback function might be an empty value, and the last non-empty line will be passed to callback function <callback> even if it has no newline marker.

func ReadLines Uses

func ReadLines(file string, callback func(text string)) error

ReadLines reads file content line by line, which is passed to the callback function <callback> as string. It matches each line of text, separated by chars '\r' or '\n', stripped any trailing end-of-line marker.

Note that the parameter passed to callback function might be an empty value, and the last non-empty line will be passed to callback function <callback> even if it has no newline marker.

func ReadableSize Uses

func ReadableSize(path string) string

ReadableSize formats size of file given by <path>, for more human readable.

func RealPath Uses

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 Uses

func Remove(path string) error

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

func Rename Uses

func Rename(src string, dst string) error

Rename is alias of Move. See Move.

func ReplaceDir Uses

func ReplaceDir(search, replace, path, pattern string, recursive ...bool) error

ReplaceDir replaces content for files under <path>. The parameter <pattern> specifies the file pattern which matches to be replaced. It does replacement recursively if given parameter <recursive> is true.

func ReplaceDirFunc Uses

func ReplaceDirFunc(f func(path, content string) string, path, pattern string, recursive ...bool) error

ReplaceDirFunc replaces content for files under <path> with callback function <f>. The parameter <pattern> specifies the file pattern which matches to be replaced. It does replacement recursively if given parameter <recursive> is true.

func ReplaceFile Uses

func ReplaceFile(search, replace, path string) error

ReplaceFile replaces content for file <path>.

func ReplaceFileFunc Uses

func ReplaceFileFunc(f func(path, content string) string, path string) error

ReplaceFileFunc replaces content for file <path> with callback function <f>.

func ScanDir Uses

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 Uses

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 Uses

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 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.

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

func ScanDirFunc Uses

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 Search(name string, prioritySearchPaths ...string) (realPath string, err error)

Search searches file by name <name> in following paths with priority: prioritySearchPaths, Pwd()、SelfDir()、MainPkgPath(). It returns the absolute file path of <name> if found, or en empty string if not found.

func SelfDir Uses

func SelfDir() string

SelfDir returns absolute directory path of current running process(binary).

func SelfName Uses

func SelfName() string

SelfName returns file name of current running process(binary).

func SelfPath Uses

func SelfPath() string

SelfPath returns absolute file path of current running process(binary).

func Size Uses

func Size(path string) int64

Size returns the size of file specified by <path> in byte.

func SortFiles Uses

func SortFiles(files []string) []string

SortFiles sorts the <files> in order of: directory -> file. Note that the item of <files> should be absolute path.

func Stat Uses

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 StrToSize Uses

func StrToSize(sizeStr string) int64

StrToSize converts formatted size string to its size in bytes.

func TempDir Uses

func TempDir(names ...string) string

TempDir retrieves and returns the temporary directory of current system. It return "/tmp" is current in *nix system, or else it returns os.TempDir().

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

func Truncate Uses

func Truncate(path string, size int) error

Truncate truncates file of <path> to given size by <size>.

Package gfile imports 24 packages (graph) and is imported by 21 packages. Updated 2020-10-11. Refresh now. Tools for package owners.