Documentation ¶
Overview ¶
Package gfile provides easy-to-use operations for file system.
Index ¶
- Constants
- func Basename(path string) string
- func Chmod(path string, mode os.FileMode) error
- func Copy(src string, dst string) error
- func CopyDir(src string, dst string) (err error)
- func CopyFile(src, dst string) (err error)
- func Create(path string) (*os.File, error)
- func Dir(path string) string
- func DirNames(path string) ([]string, error)
- func Exists(path string) bool
- func Ext(path string) string
- func FormatSize(raw float64) string
- func GetBinContents(path string) []byte
- func GetBinContentsByTwoOffsets(reader io.ReaderAt, start int64, end int64) []byte
- func GetBinContentsByTwoOffsetsByPath(path string, start int64, end int64) []byte
- func GetBinContentsTilChar(reader io.ReaderAt, char byte, start int64) ([]byte, int64)
- func GetBinContentsTilCharByPath(path string, char byte, start int64) ([]byte, int64)
- func GetContents(path string) string
- func GetNextCharOffset(reader io.ReaderAt, char byte, start int64) int64
- func GetNextCharOffsetByPath(path string, char byte, start int64) int64
- func Glob(pattern string, onlyNames ...bool) ([]string, error)
- func Home() (string, error)
- func Info(path string) (os.FileInfo, error)
- func IsDir(path string) bool
- func IsFile(path string) bool
- func IsReadable(path string) bool
- func IsWritable(path string) bool
- func MTime(path string) int64
- func MTimeMillisecond(path string) int64
- func MainPkgPath() string
- func Mkdir(path string) error
- func Move(src string, dst string) error
- func Open(path string) (*os.File, error)
- func OpenFile(path string, flag int, perm os.FileMode) (*os.File, error)
- func OpenWithFlag(path string, flag int) (*os.File, error)
- func OpenWithFlagPerm(path string, flag int, perm int) (*os.File, error)
- func PutBinContents(path string, content []byte) error
- func PutBinContentsAppend(path string, content []byte) error
- func PutContents(path string, content string) error
- func PutContentsAppend(path string, content string) error
- func Pwd() string
- func ReadableSize(path string) string
- func RealPath(path string) string
- func Remove(path string) error
- func Rename(src string, dst string) error
- func ScanDir(path string, pattern string, recursive ...bool) ([]string, error)
- func Search(name string, prioritySearchPaths ...string) (realPath string, err error)
- func SelfDir() string
- func SelfName() string
- func SelfPath() string
- func Size(path string) int64
- func Stat(path string) (os.FileInfo, error)
- func TempDir() string
- func Truncate(path string, size int) error
Constants ¶
const ( // Separator for file system. Separator = string(filepath.Separator) )
Variables ¶
This section is empty.
Functions ¶
func Basename ¶
Basename returns the last element of path. Trailing path separators are removed before extracting the last element. If the path is empty, Base returns ".". If the path consists entirely of separators, Base returns a single separator.
func Copy ¶
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 ¶
CopyDir recursively copies a directory tree, attempting to preserve permissions. Source directory must exist, destination directory must *not* exist. Symlinks are ignored and skipped.
func CopyFile ¶
CopyFile copies the contents of the file named src to the file named by dst. The file will be created if it does not already 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 ¶
Create creates file with given <path> recursively. The parameter <path> is suggested to be absolute path.
func Dir ¶
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 Ext ¶
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 FormatSize ¶
FormatSize formats size <raw> for more human readable.
func GetBinContents ¶
GetBinContents returns the file content of <path> as []byte. It returns nil if it fails reading.
func GetBinContentsByTwoOffsets ¶
GetBinContentsByTwoOffsets 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 GetBinContentsByTwoOffsetsByPath ¶
GetBinContentsByTwoOffsetsByPath 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 GetBinContentsTilChar ¶
GetBinContentsTilChar 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 GetBinContentsTilCharByPath ¶
GetBinContentsTilCharByPath 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 GetContents ¶
GetContents returns the file content of <path> as string. It returns en empty string if it fails reading.
func GetNextCharOffset ¶
GetNextCharOffset returns the file offset for given <char> starting from <start>.
func GetNextCharOffsetByPath ¶
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 ¶
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 IsReadable ¶
IsReadable checks whether given <path> is readable.
func IsWritable ¶
IsWritable checks whether given <path> is writable.
@TODO improve performance; use golang.org/x/sys to cross-plat-form
func MTimeMillisecond ¶
MTimeMillisecond returns the modification time of file given by <path> in millisecond.
func MainPkgPath ¶
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 ¶
Mkdir creates directories recursively with given <path>. The parameter <path> is suggested to be absolute path.
func OpenWithFlag ¶
OpenWithFlag opens file/directory with default perm and given <flag>.
func OpenWithFlagPerm ¶
OpenWithFlagPerm opens file/directory with given <flag> and <perm>.
func PutBinContents ¶
PutBinContents puts binary <content> to file of <path>. It creates file of <path> recursively if it does not exist.
func PutBinContentsAppend ¶
PutBinContentsAppend appends binary <content> to file of <path>. It creates file of <path> recursively if it does not exist.
func PutContents ¶
PutContents puts string <content> to file of <path>. It creates file of <path> recursively if it does not exist.
func PutContentsAppend ¶
PutContentsAppend appends string <content> to file of <path>. It creates file of <path> recursively if it does not exist.
func ReadableSize ¶
ReadableSize formats size of file given by <path>, for more human readable.
func RealPath ¶
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 ¶
Remove deletes all file/directory with <path> parameter. If parameter <path> is directory, it deletes it recursively.
func ScanDir ¶
ScanDir returns all sub-files with absolute paths of given <path>, It scans directory recursively if given parameter <recursive> is true.
func Search ¶
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 ¶
func SelfDir() string
SelfDir returns absolute directory path of current running process(binary).
func SelfName ¶ added in v1.8.1
func SelfName() string
SelfName returns file name of current running process(binary).
func SelfPath ¶
func SelfPath() string
SelfPath returns absolute file path of current running process(binary).
Types ¶
This section is empty.