Versions Compared
compared with
Key
- This line was added.
- This line was removed.
- Formatting was changed.
gfile
模块是对文件操作的进一步封装,提供了常用的,简易的API来操作底层文件,隐藏了复杂的底层实现细节。
使用方式:
import "github.com/gogf/gf/os/gfile"
接口文档:
https://godoc.org/github.com/gogf/gf/os/gfile
修改时间
MTime
- 说明:获取路径修改时间
示例:
Code Block language go func ExampleMTime() { t := gfile.MTime(gfile.TempDir()) fmt.Println(t) // May Output: // 2021-11-02 15:18:43.901141 +0800 CST }
ExampleMTimestamp
- 说明:获取路径修改时间戳(秒)
示例:
Code Block language go func ExampleMTimestamp() { t := gfile.MTimestamp(gfile.TempDir()) fmt.Println(t) // May Output: // 1635838398 }
ExampleMTimestampMilli
- 说明:获取路径修改时间戳(毫秒)
示例:
Code Block language go func ExampleMTimestampMilli() { t := gfile.MTimestampMilli(gfile.TempDir()) fmt.Println(t) // May Output: // 1635838529330 }
大小
Size
- 说明:获取路径大小
示例:
Code Block language go func ExampleSize() { tempSizeDir := gfile.TempDir() + gfile.Separator + "test_size" gfile.Mkdir(tempSizeDir) size := gfile.Size(tempSizeDir) fmt.Println(size) // Output: // 0 }
SizeFormat
- 说明:获取路径大小,并格式化
示例:
Code Block language go func ExampleSizeFormat() { tempSizeDir := gfile.TempDir() + gfile.Separator + "test_size" gfile.Mkdir(tempSizeDir) sizeStr := gfile.SizeFormat(tempSizeDir) fmt.Println(sizeStr) // Output: // 0.00B }
ReadableSize
- 说明:获取路径大小,并格式化人类易读
示例:
Code Block language go func ExampleReadableSize() { tempSizeDir := gfile.TempDir() + gfile.Separator + "test_size" gfile.Mkdir(tempSizeDir) sizeStr := gfile.ReadableSize(tempSizeDir) fmt.Println(sizeStr) // Output: // 0.00B }
StrToSize
- 说明:大小字符串转换为大小整形
示例:
Code Block language go func ExampleStrToSize() { size := gfile.StrToSize("100MB") fmt.Println(size) // Output: // 104857600 }
FormatSize
- 说明:大小整形转换为大小字符串
示例:
Code Block language go func ExampleFormatSize() { sizeStr := gfile.FormatSize(104857600) fmt.Println(sizeStr) sizeStr1 := gfile.FormatSize(999999999999999999) fmt.Println(sizeStr1) // Output: // 100.00M // 888.18P }
排序
SortFiles
- 说明:排序多个路径
示例:
Code Block language go 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] }
Panel | ||
---|---|---|
| ||
|