Documentation
¶
Index ¶
- Constants
- Variables
- func CopyFile(srcPath, dstPath string) error
- func CreateFile(fpath string, filePerm, dirPerm os.FileMode, fileFlag ...int) (*os.File, error)
- func DeleteIfExist(fPath string) error
- func DeleteIfFileExist(fPath string) error
- func Dir(fpath string) string
- func DiscardReader(src io.Reader)
- func Expand(pathStr string) string
- func ExpandPath(pathStr string) string
- func FileExists(path string) bool
- func FileExt(fpath string) string
- func FindInDir(dir string, handleFn HandleFunc, filters ...FilterFunc) (e error)
- func GetContents(in interface{}) []byte
- func GlobWithFunc(pattern string, fn func(filePath string) error) (err error)
- func IsAbsPath(aPath string) bool
- func IsDir(path string) bool
- func IsFile(path string) bool
- func IsImageFile(path string) bool
- func IsZipFile(filepath string) bool
- func MimeType(path string) (mime string)
- func MkParentDir(fpath string) error
- func Mkdir(dirPath string, perm os.FileMode) error
- func MustCopyFile(srcPath, dstPath string)
- func MustCreateFile(filePath string, filePerm, dirPerm os.FileMode) *os.File
- func MustReadFile(filePath string) []byte
- func MustReadReader(r io.Reader) []byte
- func MustRemove(fPath string)
- func Name(fpath string) string
- func OSTempDir(pattern string) (string, error)
- func OSTempFile(pattern string) (*os.File, error)
- func OpenFile(filepath string, flag int, perm os.FileMode) (*os.File, error)
- func OpenReadFile(filepath string) (*os.File, error)
- func PathExists(path string) bool
- func PathName(fpath string) string
- func PutContents(filePath string, data interface{}, fileFlag ...int) (int, error)
- func QuickOpenFile(filepath string, fileFlag ...int) (*os.File, error)
- func QuietRemove(fPath string)
- func ReadExistFile(filePath string) []byte
- func ReaderMimeType(r io.Reader) (mime string)
- func Realpath(pathStr string) string
- func Remove(fPath string) error
- func RmFileIfExist(fPath string) error
- func RmIfExist(fPath string) error
- func SplitPath(pathStr string) (dir, name string)
- func Suffix(fpath string) string
- func TempDir(dir, pattern string) (string, error)
- func TempFile(dir, pattern string) (*os.File, error)
- func Unzip(archive, targetDir string) (err error)
- func WriteFile(filePath string, data interface{}, perm os.FileMode, fileFlag ...int) error
- type FilterFunc
- type HandleFunc
Constants ¶
const ( FsCWAFlags = os.O_CREATE | os.O_WRONLY | os.O_APPEND // create, append write-only FsCWTFlags = os.O_CREATE | os.O_WRONLY | os.O_TRUNC // create, override write-only FsCWFlags = os.O_CREATE | os.O_WRONLY // create, write-only FsRFlags = os.O_RDONLY // read-only )
some flag consts for open file
const (
// MimeSniffLen sniff Length, use for detect file mime type
MimeSniffLen = 512
)
Variables ¶
var ( // DefaultDirPerm perm and flags for create log file DefaultDirPerm os.FileMode = 0775 DefaultFilePerm os.FileMode = 0665 OnlyReadFilePerm os.FileMode = 0444 // DefaultFileFlags for create and write DefaultFileFlags = os.O_CREATE | os.O_WRONLY | os.O_APPEND // OnlyReadFileFlags open file for read OnlyReadFileFlags = os.O_RDONLY )
var ( DirExist = IsDir FileExist = IsFile PathExist = PathExists )
alias methods
var ( // MustRm removes the named file or (empty) directory. MustRm = MustRemove // QuietRm removes the named file or (empty) directory. QuietRm = QuietRemove )
alias methods
var ImageMimeTypes = map[string]string{
"bmp": "image/bmp",
"gif": "image/gif",
"ief": "image/ief",
"jpg": "image/jpeg",
"jpeg": "image/jpeg",
"png": "image/png",
"svg": "image/svg+xml",
"ico": "image/x-icon",
"webp": "image/webp",
}
ImageMimeTypes refer net/http package
Functions ¶
func CreateFile ¶ added in v0.2.10
CreateFile create file if not exists
Usage:
CreateFile("path/to/file.txt", 0664, 0666)
func DeleteIfExist ¶ added in v0.3.11
DeleteIfExist removes the named file or (empty) directory on exists.
func DeleteIfFileExist ¶ added in v0.2.10
DeleteIfFileExist removes the named file on exists.
func DiscardReader ¶ added in v0.4.5
DiscardReader anything from the reader
func ExpandPath ¶ added in v0.3.9
ExpandPath will parse `~` as user home dir path.
func FileExists ¶
FileExists reports whether the named file or directory exists.
func FindInDir ¶ added in v0.5.3
func FindInDir(dir string, handleFn HandleFunc, filters ...FilterFunc) (e error)
FindInDir code refer the go pkg: path/filepath.glob() filters: return false will skip the file.
func GetContents ¶ added in v0.5.3
func GetContents(in interface{}) []byte
GetContents read contents from path or io.Reader, will panic on error
func GlobWithFunc ¶ added in v0.5.2
GlobWithFunc handle matched file
func IsImageFile ¶ added in v0.2.5
IsImageFile check file is image file.
func IsZipFile ¶
IsZipFile check is zip file. from https://blog.csdn.net/wangshubo1989/article/details/71743374
func MkParentDir ¶ added in v0.3.11
MkParentDir quick create parent dir
func MustCopyFile ¶ added in v0.3.8
func MustCopyFile(srcPath, dstPath string)
MustCopyFile copy file to another path.
func MustCreateFile ¶ added in v0.3.8
MustCreateFile create file, will panic on error
func MustReadFile ¶ added in v0.3.7
MustReadFile read file contents, will panic on error
func MustReadReader ¶ added in v0.4.3
MustReadReader read contents from io.Reader, will panic on error
func MustRemove ¶ added in v0.3.11
func MustRemove(fPath string)
MustRemove removes the named file or (empty) directory. NOTICE: will panic on error
func OSTempDir ¶ added in v0.3.12
OSTempDir creates a new temp dir on os.TempDir and return the temp dir path
Usage:
fsutil.OSTempDir("example.*")
func OSTempFile ¶ added in v0.3.12
OSTempFile create a temp file on os.TempDir()
Usage:
fsutil.OSTempFile("example.*.txt")
func OpenReadFile ¶ added in v0.5.3
OpenReadFile like os.OpenFile, open file for read contents
func PathExists ¶ added in v0.2.1
PathExists reports whether the named file or directory exists.
func PutContents ¶ added in v0.5.3
PutContents create file and write contents to file at once.
data type allow: string, []byte, io.Reader
func QuickOpenFile ¶ added in v0.3.8
QuickOpenFile like os.OpenFile, open for write, if not exists, will create it.
Tip: file flag default is FsCWAFlags
func QuietRemove ¶ added in v0.3.11
func QuietRemove(fPath string)
QuietRemove removes the named file or (empty) directory.
NOTICE: will ignore error
func ReadExistFile ¶ added in v0.3.11
ReadExistFile read file contents if existed, will panic on error
func ReaderMimeType ¶ added in v0.2.5
ReaderMimeType get the io.Reader mimeType
Usage:
file, err := os.Open(filepath)
if err != nil {
return
}
mime := ReaderMimeType(file)
func Realpath ¶ added in v0.4.0
Realpath returns the shortest path name equivalent to path by purely lexical processing.
func RmFileIfExist ¶ added in v0.5.3
RmFileIfExist removes the named file on exists.
func SplitPath ¶ added in v0.5.6
SplitPath splits path immediately following the final Separator, separating it into a directory and file name component
func TempDir ¶ added in v0.3.12
TempDir creates a new temp dir and return the temp dir path
Usage:
fsutil.TempDir("", "example.*")
fsutil.TempDir("testdata", "example.*")
func TempFile ¶ added in v0.3.12
TempFile is like ioutil.TempFile, but can custom temp dir.
Usage:
fsutil.TempFile("", "example.*.txt")
func Unzip ¶
Unzip a zip archive from https://blog.csdn.net/wangshubo1989/article/details/71743374
Types ¶
type FilterFunc ¶ added in v0.3.8
filter and handle func for FindInDir