gf: github.com/johng-cn/gf/g/os/gfile Index | Files

package gfile

import "github.com/johng-cn/gf/g/os/gfile"

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

Index

Package Files

gfile.go gfile_contents.go gfile_search.go gfile_size.go gfile_time.go

Constants

const (
    // Separator for file system.
    Separator = string(filepath.Separator)
)

func Basename Uses

func Basename(path string) string

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 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 from <src> to <dst>.

@TODO directory copy support.

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

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

func FormatSize(raw float64) string

FormatSize formats size <raw> for more human readable.

func GetBinContents Uses

func GetBinContents(path string) []byte

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

func GetBinContentsByTwoOffsets Uses

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

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 Uses

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

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 Uses

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

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 Uses

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

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 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 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() (string, error)

Home returns absolute path of current user's home directory.

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.

func IsFile Uses

func IsFile(path string) bool

IsFile checks whether given <path> a file, which means it's not a directory.

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

func MTime(path string) int64

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

func MTimeMillisecond Uses

func MTimeMillisecond(path string) int64

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

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 given <flag> and <perm>.

func OpenWithFlag Uses

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

OpenWithFlag opens file/directory with default perm and given <flag>.

func OpenWithFlagPerm Uses

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

OpenWithFlagPerm opens file/directory with given <flag> and <perm>.

func PutBinContents Uses

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

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

func PutBinContentsAppend Uses

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

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

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

Alias of Move. See Move.

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.

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

func TempDir() string

See os.TempDir().

func Truncate Uses

func Truncate(path string, size int) error

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

Package gfile imports 18 packages (graph). Updated 2019-06-19. Refresh now. Tools for package owners.