From dcdb29334d9cbf6bffafb028ce1dd3449fe2a726 Mon Sep 17 00:00:00 2001 From: dudaodong Date: Wed, 26 Apr 2023 10:44:12 +0800 Subject: [PATCH] doc: update fileutils package document --- docs/fileutil.md | 141 ++++++++++++++++++++++++++++++++++++++++- docs/fileutil_zh-CN.md | 140 +++++++++++++++++++++++++++++++++++++++- fileutil/file.go | 4 +- 3 files changed, 280 insertions(+), 5 deletions(-) diff --git a/docs/fileutil.md b/docs/fileutil.md index 1b9b949..e61d485 100644 --- a/docs/fileutil.md +++ b/docs/fileutil.md @@ -39,6 +39,10 @@ import ( - [Zip](#Zip) - [UnZip](#UnZip) - [IsZipFile](#IsZipFile) +- [FileSize](#FileSize) +- [MTime](#MTime) +- [Sha](#Sha) +- [ReadCsvFile](#ReadCsvFile)
@@ -497,7 +501,6 @@ func main() { } ``` - ### IsZipFile

Checks if file is zip file or not.

@@ -522,4 +525,138 @@ func main() { isZip := IsZipFile("./zipfile.zip") fmt.Println(isZip) } -``` \ No newline at end of file +``` + +### FileSize + +

Returns file size in bytes.

+ +Signature: + +```go +func FileSize(path string) (int64, error) +``` + +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/fileutil" +) + +func main() { + size, err := fileutil.FileSize("./testdata/test.txt") + + fmt.Println(size) + fmt.Println(err) + + // Output: + // 20 + // +} +``` + +### MTime + +

Returns file modified time(unix timestamp).

+ +Signature: + +```go +func MTime(filepath string) (int64, error) +``` + +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/fileutil" +) + +func main() { + mtime, err := fileutil.MTime("./testdata/test.txt") + + fmt.Println(mtime) + fmt.Println(err) + + // Output: + // 1682391110 + // +} +``` + +### Sha + +

returns file sha value, param `shaType` should be 1, 256 or 512.

+ +Signature: + +```go +func Sha(filepath string, shaType ...int) (string, error) +``` + +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/fileutil" +) + +func main() { + sha1, err := fileutil.Sha("./testdata/test.txt", 1) + sha256, _ := fileutil.Sha("./testdata/test.txt", 256) + sha512, _ := fileutil.Sha("./testdata/test.txt", 512) + + fmt.Println(sha1) + fmt.Println(sha256) + fmt.Println(sha512) + fmt.Println(err) + + // Output: + // dda3cf10c5a6ff6c6659a497bf7261b287af2bc7 + // aa6d0a3fbc3442c228d606da09e0c1dc98c69a1cac3da1909199e0266171df35 + // d22aba2a1b7a2e2f512756255cc1c3708905646920cb1eb95e45b531ba74774dbbb89baebf1f716220eb9cf4908f1cfc5b2a01267704d9a59f59d77cab609870 + // +} +``` + +### ReadCsvFile + +

Reads file content into slice.

+ +Signature: + +```go +func ReadCsvFile(filepath string) ([][]string, error) +``` + +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/fileutil" +) + +func main() { + content, err := fileutil.ReadCsvFile("./testdata/test.csv") + + fmt.Println(content) + fmt.Println(err) + + // Output: + // [[Bob 12 male] [Duke 14 male] [Lucy 16 female]] + // +} +``` diff --git a/docs/fileutil_zh-CN.md b/docs/fileutil_zh-CN.md index 61a63cf..3852549 100644 --- a/docs/fileutil_zh-CN.md +++ b/docs/fileutil_zh-CN.md @@ -39,6 +39,10 @@ import ( - [Zip](#Zip) - [UnZip](#UnZip) - [IsZipFile](#IsZipFile) +- [FileSize](#FileSize) +- [MTime](#MTime) +- [Sha](#Sha) +- [ReadCsvFile](#ReadCsvFile)
@@ -108,7 +112,7 @@ func main() { func CreateDir(absPath string) error ``` -Example: +示例: ```go package main @@ -522,3 +526,137 @@ func main() { fmt.Println(isZip) } ``` + +### FileSize + +

返回文件字节大小。

+ +函数签名: + +```go +func FileSize(path string) (int64, error) +``` + +示例: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/fileutil" +) + +func main() { + size, err := fileutil.FileSize("./testdata/test.txt") + + fmt.Println(size) + fmt.Println(err) + + // Output: + // 20 + // +} +``` + +### MTime + +

返回文件修改时间(unix timestamp).

+ +函数签名: + +```go +func MTime(filepath string) (int64, error) +``` + +示例: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/fileutil" +) + +func main() { + mtime, err := fileutil.MTime("./testdata/test.txt") + + fmt.Println(mtime) + fmt.Println(err) + + // Output: + // 1682391110 + // +} +``` + +### Sha + +

返回文件sha值,参数`shaType` 应传值为: 1, 256,512.

+ +函数签名: + +```go +func Sha(filepath string, shaType ...int) (string, error) +``` + +示例: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/fileutil" +) + +func main() { + sha1, err := fileutil.Sha("./testdata/test.txt", 1) + sha256, _ := fileutil.Sha("./testdata/test.txt", 256) + sha512, _ := fileutil.Sha("./testdata/test.txt", 512) + + fmt.Println(sha1) + fmt.Println(sha256) + fmt.Println(sha512) + fmt.Println(err) + + // Output: + // dda3cf10c5a6ff6c6659a497bf7261b287af2bc7 + // aa6d0a3fbc3442c228d606da09e0c1dc98c69a1cac3da1909199e0266171df35 + // d22aba2a1b7a2e2f512756255cc1c3708905646920cb1eb95e45b531ba74774dbbb89baebf1f716220eb9cf4908f1cfc5b2a01267704d9a59f59d77cab609870 + // +} +``` + +### ReadCsvFile + +

读取csv文件内容到切片

+ +函数签名: + +```go +func ReadCsvFile(filepath string) ([][]string, error) +``` + +示例: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/fileutil" +) + +func main() { + content, err := fileutil.ReadCsvFile("./testdata/test.csv") + + fmt.Println(content) + fmt.Println(err) + + // Output: + // [[Bob 12 male] [Duke 14 male] [Lucy 16 female]] + // +} +``` diff --git a/fileutil/file.go b/fileutil/file.go index 0f3fd84..174a46e 100644 --- a/fileutil/file.go +++ b/fileutil/file.go @@ -402,7 +402,7 @@ func MTime(filepath string) (int64, error) { return f.ModTime().Unix(), nil } -// MTime returns file modified time, param `shaType` should be 1, 256 or 512. +// MTime returns file sha value, param `shaType` should be 1, 256 or 512. // Play: todo func Sha(filepath string, shaType ...int) (string, error) { file, err := os.Open(filepath) @@ -436,7 +436,7 @@ func Sha(filepath string, shaType ...int) (string, error) { } -// MTime returns file modified time. +// ReadCsvFile read file content into slice. // Play: todo func ReadCsvFile(filepath string) ([][]string, error) { f, err := os.Open(filepath)