Documentation ¶
Overview ¶
Package fileutil implements some basic functions for file operations
Index ¶
- func ClearFile(path string) error
- func CopyFile(srcPath string, dstPath string) error
- func CreateDir(absPath string) error
- func CreateFile(path string) bool
- func CurrentPath() string
- func FileMode(path string) (fs.FileMode, error)
- func FileSize(path string) (int64, error)
- func IsDir(path string) bool
- func IsExist(path string) bool
- func IsLink(path string) bool
- func IsZipFile(filepath string) bool
- func ListFileNames(path string) ([]string, error)
- func MTime(filepath string) (int64, error)
- func MiMeType(file any) string
- func ReadCsvFile(filepath string) ([][]string, error)
- func ReadFileByLine(path string) ([]string, error)
- func ReadFileToString(path string) (string, error)
- func RemoveFile(path string) error
- func Sha(filepath string, shaType ...int) (string, error)
- func UnZip(zipFile string, destPath string) error
- func Zip(fpath string, destPath string) error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ClearFile ¶
ClearFile write empty string to path file. Play: https://go.dev/play/p/NRZ0ZT-G94H
Example ¶
fname := "./test.txt" CreateFile(fname) f, _ := os.OpenFile(fname, os.O_WRONLY|os.O_TRUNC, 0777) defer f.Close() _, err := f.WriteString("hello world") if err != nil { return } content1, _ := ReadFileToString(fname) err = ClearFile(fname) if err != nil { return } content2, _ := ReadFileToString(fname) os.Remove(fname) fmt.Println(content1) fmt.Println(content2)
Output: hello world
func CopyFile ¶
CopyFile copy src file to dest file. Play: https://go.dev/play/p/Jg9AMJMLrJi
func CreateDir ¶
CreateDir create directory in absolute path. param `absPath` like /a/, /a/b/. Play: https://go.dev/play/p/qUuCe1OGQnM
Example ¶
pwd, _ := os.Getwd() dirPath := pwd + "/test_xxx/" result1 := IsExist(dirPath) err := CreateDir(dirPath) if err != nil { return } result2 := IsExist(dirPath) os.Remove(dirPath) fmt.Println(result1) fmt.Println(result2)
Output: false true
func CreateFile ¶
CreateFile create a file in path. Play: https://go.dev/play/p/lDt8PEsTNKI
Example ¶
fname := "./a.txt" result1 := IsExist(fname) CreateFile(fname) result2 := IsExist(fname) os.Remove(fname) fmt.Println(result1) fmt.Println(result2)
Output: false true
func CurrentPath ¶
func CurrentPath() string
CurrentPath return current absolute path. Play: https://go.dev/play/p/s74a9iBGcSw
func FileMode ¶
FileMode return file's mode and permission. Play: https://go.dev/play/p/2l2hI42fA3p
func FileSize ¶
FileSize returns file size in bytes. Play: todo
Example ¶
size, err := FileSize("./testdata/test.txt") fmt.Println(size) fmt.Println(err)
Output: 20 <nil>
func IsDir ¶
IsDir checks if the path is directory or not. Play: https://go.dev/play/p/WkVwEKqtOWk
Example ¶
result1 := IsDir("./") result2 := IsDir("./xxx.go") fmt.Println(result1) fmt.Println(result2)
Output: true false
func IsExist ¶
IsExist checks if a file or directory exists. Play: https://go.dev/play/p/nKKXt8ZQbmh
Example ¶
result1 := IsExist("./") result2 := IsExist("./xxx.go") fmt.Println(result1) fmt.Println(result2)
Output: true false
func IsLink ¶
IsLink checks if a file is symbol link or not. Play: https://go.dev/play/p/TL-b-Kzvf44
func IsZipFile ¶
IsZipFile checks if file is zip or not. Play: todo
Example ¶
result1 := IsZipFile("./file.go") result2 := IsZipFile("./testdata/file.go.zip") fmt.Println(result1) fmt.Println(result2)
Output: false true
func ListFileNames ¶
ListFileNames return all file names in the path. Play: https://go.dev/play/p/Tjd7Y07rejl
Example ¶
fileList, _ := ListFileNames("./") fmt.Println(fileList)
Output: [file.go file_example_test.go file_test.go]
func MiMeType ¶
MiMeType return file mime type param `file` should be string(file path) or *os.File. Play: https://go.dev/play/p/bd5sevSUZNu
func ReadCsvFile ¶
ReadCsvFile read file content into slice. Play: todo
Example ¶
content, err := ReadCsvFile("./testdata/test.csv") fmt.Println(content) fmt.Println(err)
Output: [[Bob 12 male] [Duke 14 male] [Lucy 16 female]] <nil>
func ReadFileByLine ¶
ReadFileByLine read file line by line. Play: https://go.dev/play/p/svJP_7ZrBrD
Example ¶
fname := "./test.txt" CreateFile(fname) f, _ := os.OpenFile(fname, os.O_WRONLY|os.O_TRUNC, 0777) defer f.Close() _, err := f.WriteString("hello\nworld") if err != nil { return } content, _ := ReadFileByLine(fname) os.Remove(fname) fmt.Println(content)
Output: [hello world]
func ReadFileToString ¶
ReadFileToString return string of file content. Play: https://go.dev/play/p/cmfwp_5SQTp
Example ¶
fname := "./test.txt" CreateFile(fname) f, _ := os.OpenFile(fname, os.O_WRONLY|os.O_TRUNC, 0777) defer f.Close() _, err := f.WriteString("hello world") if err != nil { return } content, _ := ReadFileToString(fname) os.Remove(fname) fmt.Println(content)
Output: hello world
func RemoveFile ¶
RemoveFile remove the path file. Play: https://go.dev/play/p/P2y0XW8a1SH
Example ¶
srcFile := "./text.txt" CreateFile(srcFile) copyFile := "./text_copy.txt" err := CopyFile(srcFile, copyFile) if err != nil { return } file, err := os.Open(copyFile) if err != nil { return } result1 := IsExist(copyFile) result2 := file.Name() os.Remove(srcFile) os.Remove(copyFile) fmt.Println(result1) fmt.Println(result2)
Output: true ./text_copy.txt
func Sha ¶
MTime returns file sha value, param `shaType` should be 1, 256 or 512. Play: todo
Example ¶
sha1, err := Sha("./testdata/test.txt", 1) sha256, _ := Sha("./testdata/test.txt", 256) sha512, _ := Sha("./testdata/test.txt", 512) fmt.Println(sha1) fmt.Println(sha256) fmt.Println(sha512) fmt.Println(err)
Output: dda3cf10c5a6ff6c6659a497bf7261b287af2bc7 aa6d0a3fbc3442c228d606da09e0c1dc98c69a1cac3da1909199e0266171df35 d22aba2a1b7a2e2f512756255cc1c3708905646920cb1eb95e45b531ba74774dbbb89baebf1f716220eb9cf4908f1cfc5b2a01267704d9a59f59d77cab609870 <nil>
func UnZip ¶
UnZip unzip the file and save it to destPath. Play: https://go.dev/play/p/g0w34kS7B8m
Example ¶
fname := "./test.txt" file, _ := os.Create(fname) _, err := file.WriteString("hello\nworld") if err != nil { return } f, _ := os.Open(fname) defer f.Close() mimeType := MiMeType(f) fmt.Println(mimeType) os.Remove(fname)
Output: application/octet-stream
func Zip ¶
Zip create zip file, fpath could be a single file or a directory. Play: https://go.dev/play/p/j-3sWBp8ik_P
Example ¶
srcFile := "./test.txt" CreateFile(srcFile) zipFile := "./test.zip" err := Zip(srcFile, zipFile) if err != nil { return } result := IsExist(zipFile) os.Remove(srcFile) os.Remove(zipFile) fmt.Println(result)
Output: true
Types ¶
This section is empty.