gfile
模块是对文件操作的进一步封装,提供了常用的,简易的API来操作底层文件,隐藏了复杂的底层实现细节。
使用方式:
import "github.com/gogf/gf/os/gfile"
接口文档:
https://godoc.org/github.com/gogf/gf/os/gfile
示例:
func ExampleMTime() { t := gfile.MTime(gfile.TempDir()) fmt.Println(t) // May Output: // 2021-11-02 15:18:43.901141 +0800 CST } |
示例:
func ExampleMTimestamp() { t := gfile.MTimestamp(gfile.TempDir()) fmt.Println(t) // May Output: // 1635838398 } |
示例:
func ExampleMTimestampMilli() { t := gfile.MTimestampMilli(gfile.TempDir()) fmt.Println(t) // May Output: // 1635838529330 } |
示例:
func ExampleSize() { tempDir := gfile.TempDir("gfile_example") gfile.Mkdir(tempDir) size := gfile.Size(tempDir) fmt.Println(size) // Output: // 0 } |
示例:
func ExampleSizeFormat() { tempDir := gfile.TempDir("gfile_example") gfile.Mkdir(tempDir) sizeStr := gfile.SizeFormat(tempDir) fmt.Println(sizeStr) // Output: // 0.00B } |
示例:
func ExampleReadableSize() { tempDir := gfile.TempDir("gfile_example") gfile.Mkdir(tempDir) sizeStr := gfile.ReadableSize(tempDir) fmt.Println(sizeStr) // Output: // 0.00B } |
示例:
func ExampleStrToSize() { size := gfile.StrToSize("100MB") fmt.Println(size) // Output: // 104857600 } |
示例:
func ExampleFormatSize() { sizeStr := gfile.FormatSize(104857600) fmt.Println(sizeStr) sizeStr1 := gfile.FormatSize(999999999999999999) fmt.Println(sizeStr1) // Output: // 100.00M // 888.18P } |
示例:
func ExampleSortFiles() { files := []string{ "/aaa/bbb/ccc.txt", "/aaa/bbb/", "/aaa/", "/aaa", "/aaa/ccc/ddd.txt", "/bbb", "/0123", "/ddd", "/ccc", } sortOut := gfile.SortFiles(files) fmt.Println(sortOut) // Output: // [/0123 /aaa /aaa/ /aaa/bbb/ /aaa/bbb/ccc.txt /aaa/ccc/ddd.txt /bbb /ccc /ddd] } |
示例:
func ExampleSearch() { // init fileName := "123.txt" tempDir := gfile.TempDir("gfile_example") tempFile := gfile.Join(tempDir, fileName) gfile.Mkdir(tempDir) gfile.Create(tempFile) // search file realPath, _ := gfile.Search(fileName, tempDir) fmt.Println(gfile.Basename(realPath)) // Output: // 123.txt } |
示例:
func ExampleScanDir() { // init fileName := "123.txt" tempDir := gfile.TempDir("gfile_example") tempFile := gfile.Join(tempDir, fileName) tempSubDir := gfile.Join(tempDir, "sub_dir") tempSubFile := gfile.Join(tempSubDir, fileName) gfile.Mkdir(tempSubDir) gfile.Create(tempFile) gfile.Create(tempSubFile) // scans directory recursively list, _ := gfile.ScanDir(tempDir, "123.txt,sub_dir", true) for _, v := range list { fmt.Println(gfile.Basename(v)) } // Output: // 123.txt // sub_dir // 123.txt } |
示例:
func ExampleScanDirFile() { // init fileName := "123.txt" tempDir := gfile.TempDir("gfile_example") tempFile := gfile.Join(tempDir, fileName) tempSubDir := gfile.Join(tempDir, "sub_dir") tempSubFile := gfile.Join(tempSubDir, fileName) gfile.Mkdir(tempSubDir) gfile.Create(tempFile) gfile.Create(tempSubFile) // scans directory recursively exclusive of directories list, _ := gfile.ScanDirFile(tempDir, "123.txt,sub_dir", true) for _, v := range list { fmt.Println(gfile.Basename(v)) } // Output: // 123.txt // 123.txt } |
示例:
func ExampleScanDirFunc() { // init fileName := "123.txt" fileName1 := "1234.txt" tempDir := gfile.TempDir("gfile_example_1") tempFile := gfile.Join(tempDir, fileName) tempSubDir := gfile.Join(tempDir, "sub_dir") tempSubFile := gfile.Join(tempSubDir, fileName1) gfile.Mkdir(tempSubDir) gfile.Create(tempFile) gfile.Create(tempSubFile) // scans directory recursively list, _ := gfile.ScanDirFunc(tempDir, "123.txt,1234.txt,sub_dir", true, func(path string) string { // ignores some files if gfile.Basename(path) == "1234.txt" { return "" } return path }) for _, v := range list { fmt.Println(gfile.Basename(v)) } // Output: // 123.txt // sub_dir } |
示例:
func ExampleScanDirFileFunc() { // init fileName := "123.txt" fileName1 := "1234.txt" tempDir := gfile.TempDir("gfile_example_1") tempFile := gfile.Join(tempDir, fileName) tempSubDir := gfile.Join(tempDir, "sub_dir") tempSubFile := gfile.Join(tempSubDir, fileName1) gfile.Mkdir(tempSubDir) gfile.Create(tempFile) gfile.Create(tempSubFile) // scans directory recursively exclusive of directories list, _ := gfile.ScanDirFileFunc(tempDir, "123.txt,1234.txt,sub_dir", true, func(path string) string { // ignores some files if gfile.Basename(path) == "1234.txt" { return "" } return path }) for _, v := range list { fmt.Println(gfile.Basename(v)) } // Output: // 123.txt } |