From 8a9dd169e2c6fbc8256099734e889b99e93300c2 Mon Sep 17 00:00:00 2001 From: hailaz <739476267@qq.com> Date: Fri, 5 Nov 2021 14:30:50 +0800 Subject: [PATCH 01/10] test: add gflie example --- os/gfile/gfile_home.go | 7 +- os/gfile/gfile_z_contents_test.go | 2 +- os/gfile/gfile_z_example_cache_test.go | 43 ++++ os/gfile/gfile_z_example_contents_test.go | 253 ++++++++++++++++++++++ os/gfile/gfile_z_example_copy_test.go | 53 +++++ os/gfile/gfile_z_example_home_test.go | 16 ++ os/gfile/gfile_z_example_replace_test.go | 145 +++++++++++++ os/gfile/gfile_z_example_scan_test.go | 117 ++++++++++ os/gfile/gfile_z_example_search_test.go | 23 ++ os/gfile/gfile_z_example_size_test.go | 56 +++++ os/gfile/gfile_z_example_sort_test.go | 26 +++ os/gfile/gfile_z_example_time_test.go | 31 +++ os/gfile/gfile_z_test.go | 2 +- 13 files changed, 769 insertions(+), 5 deletions(-) create mode 100644 os/gfile/gfile_z_example_cache_test.go create mode 100644 os/gfile/gfile_z_example_contents_test.go create mode 100644 os/gfile/gfile_z_example_copy_test.go create mode 100644 os/gfile/gfile_z_example_home_test.go create mode 100644 os/gfile/gfile_z_example_replace_test.go create mode 100644 os/gfile/gfile_z_example_scan_test.go create mode 100644 os/gfile/gfile_z_example_search_test.go create mode 100644 os/gfile/gfile_z_example_size_test.go create mode 100644 os/gfile/gfile_z_example_sort_test.go create mode 100644 os/gfile/gfile_z_example_time_test.go diff --git a/os/gfile/gfile_home.go b/os/gfile/gfile_home.go index a6137b156..68bc6c645 100644 --- a/os/gfile/gfile_home.go +++ b/os/gfile/gfile_home.go @@ -8,13 +8,14 @@ package gfile import ( "bytes" - "github.com/gogf/gf/v2/errors/gcode" - "github.com/gogf/gf/v2/errors/gerror" "os" "os/exec" "os/user" "runtime" "strings" + + "github.com/gogf/gf/v2/errors/gcode" + "github.com/gogf/gf/v2/errors/gerror" ) // Home returns absolute path of current user's home directory. @@ -37,7 +38,7 @@ func getHomePath() (string, error) { if nil == err { return u.HomeDir, nil } - if "windows" == runtime.GOOS { + if runtime.GOOS == "windows" { return homeWindows() } return homeUnix() diff --git a/os/gfile/gfile_z_contents_test.go b/os/gfile/gfile_z_contents_test.go index 488cdfd22..9f34b2240 100644 --- a/os/gfile/gfile_z_contents_test.go +++ b/os/gfile/gfile_z_contents_test.go @@ -102,8 +102,8 @@ func Test_Truncate(t *testing.T) { t.Assert(err, nil) files, err = os.Open(testpath() + filepaths1) - defer files.Close() t.Assert(err, nil) + defer files.Close() fileinfo, err2 := files.Stat() t.Assert(err2, nil) t.Assert(fileinfo.Size(), 10) diff --git a/os/gfile/gfile_z_example_cache_test.go b/os/gfile/gfile_z_example_cache_test.go new file mode 100644 index 000000000..f7939a960 --- /dev/null +++ b/os/gfile/gfile_z_example_cache_test.go @@ -0,0 +1,43 @@ +package gfile_test + +import ( + "fmt" + "time" + + "github.com/gogf/gf/v2/os/gfile" +) + +func ExampleGetContentsWithCache() { + // init + fileName := "123.txt" + tempDir := gfile.TempDir("gfile_example_cache") + tempFile := gfile.Join(tempDir, fileName) + + gfile.Mkdir(tempDir) + gfile.Create(tempFile) + + // write contents + gfile.PutContents(tempFile, "test contents") + + // read contents + content := gfile.GetContentsWithCache(tempFile, time.Minute) + fmt.Println(content) + + time.Sleep(time.Second * 1) + + // read contents + content1 := gfile.GetContentsWithCache(tempFile) + fmt.Println(content1) + + // write new contents will clear its cache + gfile.PutContents(tempFile, "new test contents") + + // read contents + content2 := gfile.GetContentsWithCache(tempFile) + fmt.Println(content2) + + // Output: + // test contents + // test contents + // new test contents +} diff --git a/os/gfile/gfile_z_example_contents_test.go b/os/gfile/gfile_z_example_contents_test.go new file mode 100644 index 000000000..7d325e277 --- /dev/null +++ b/os/gfile/gfile_z_example_contents_test.go @@ -0,0 +1,253 @@ +package gfile_test + +import ( + "fmt" + + "github.com/gogf/gf/v2/os/gfile" +) + +func ExampleGetContents() { + // init + fileName := "123.txt" + tempDir := gfile.TempDir("gfile_example_content") + tempFile := gfile.Join(tempDir, fileName) + + gfile.Mkdir(tempDir) + gfile.Create(tempFile) + + // write contents + gfile.PutContents(tempFile, "test contents") + + // read contents + content := gfile.GetContents(tempFile) + fmt.Println(content) + + // Output: + // test contents +} + +func ExampleGetBytes() { + // init + fileName := "123.txt" + tempDir := gfile.TempDir("gfile_example_content") + tempFile := gfile.Join(tempDir, fileName) + + gfile.Mkdir(tempDir) + gfile.Create(tempFile) + + // write contents + gfile.PutContents(tempFile, "test contents") + + // read contents + content := gfile.GetBytes(tempFile) + fmt.Println(string(content)) + + // Output: + // test contents +} + +func ExamplePutContents() { + // init + fileName := "123.txt" + tempDir := gfile.TempDir("gfile_example_content") + tempFile := gfile.Join(tempDir, fileName) + + gfile.Mkdir(tempDir) + gfile.Create(tempFile) + + // write contents + gfile.PutContents(tempFile, "test contents") + + // read contents + content := gfile.GetContents(tempFile) + fmt.Println(content) + + // Output: + // test contents +} + +func ExamplePutBytes() { + // init + fileName := "123.txt" + tempDir := gfile.TempDir("gfile_example_content") + tempFile := gfile.Join(tempDir, fileName) + + gfile.Mkdir(tempDir) + gfile.Create(tempFile) + + // write contents + gfile.PutBytes(tempFile, []byte("test contents")) + + // read contents + content := gfile.GetContents(tempFile) + fmt.Println(content) + + // Output: + // test contents +} + +func ExamplePutContentsAppend() { + // init + fileName := "123.txt" + tempDir := gfile.TempDir("gfile_example_content") + tempFile := gfile.Join(tempDir, fileName) + + gfile.Mkdir(tempDir) + gfile.Create(tempFile) + + // write contents + gfile.PutContents(tempFile, "test contents") + + // read contents + content := gfile.GetContents(tempFile) + fmt.Println(content) + + // write contents + gfile.PutContentsAppend(tempFile, " append") + + // read contents + content1 := gfile.GetContents(tempFile) + fmt.Println(content1) + + // Output: + // test contents + // test contents append +} + +func ExamplePutBytesAppend() { + // init + fileName := "123.txt" + tempDir := gfile.TempDir("gfile_example_content") + tempFile := gfile.Join(tempDir, fileName) + + gfile.Mkdir(tempDir) + gfile.Create(tempFile) + + // write contents + gfile.PutBytes(tempFile, []byte("test contents")) + + // read contents + content := gfile.GetContents(tempFile) + fmt.Println(content) + + // write contents + gfile.PutBytesAppend(tempFile, []byte(" append")) + + // read contents + content1 := gfile.GetContents(tempFile) + fmt.Println(content1) + + // Output: + // test contents + // test contents append +} + +func ExampleGetNextCharOffsetByPath() { + // init + fileName := "123.txt" + tempDir := gfile.TempDir("gfile_example_content") + tempFile := gfile.Join(tempDir, fileName) + + gfile.Mkdir(tempDir) + gfile.Create(tempFile) + + // write contents + gfile.PutContents(tempFile, "test contents index") + + // read contents + index := gfile.GetNextCharOffsetByPath(tempFile, 'i', 0) + fmt.Println(index) + + // Output: + // 14 +} + +func ExampleGetBytesTilCharByPath() { + // init + fileName := "123.txt" + tempDir := gfile.TempDir("gfile_example_content") + tempFile := gfile.Join(tempDir, fileName) + + gfile.Mkdir(tempDir) + gfile.Create(tempFile) + + // write contents + gfile.PutContents(tempFile, "test contents: hello") + + // read contents + contents, index := gfile.GetBytesTilCharByPath(tempFile, ':', 0) + fmt.Println(string(contents)) + fmt.Println(index) + + // Output: + // test contents: + // 13 +} + +func ExampleGetBytesByTwoOffsetsByPath() { + // init + fileName := "123.txt" + tempDir := gfile.TempDir("gfile_example_content") + tempFile := gfile.Join(tempDir, fileName) + + gfile.Mkdir(tempDir) + gfile.Create(tempFile) + + // write contents + gfile.PutContents(tempFile, "test contents") + + // read contents + contents := gfile.GetBytesByTwoOffsetsByPath(tempFile, 0, 4) + fmt.Println(string(contents)) + + // Output: + // test +} + +func ExampleReadLines() { + // init + fileName := "123.txt" + tempDir := gfile.TempDir("gfile_example_content") + tempFile := gfile.Join(tempDir, fileName) + + gfile.Mkdir(tempDir) + gfile.Create(tempFile) + + // write contents + gfile.PutContents(tempFile, "test contents\ntest contents") + + // read contents + gfile.ReadLines(tempFile, func(text string) error { + // Process each line + fmt.Println(text) + return nil + }) + + // Output: + // test contents + // test contents +} + +func ExampleReadLinesBytes() { + // init + fileName := "123.txt" + tempDir := gfile.TempDir("gfile_example_content") + tempFile := gfile.Join(tempDir, fileName) + + gfile.Mkdir(tempDir) + gfile.Create(tempFile) + + // write contents + gfile.PutContents(tempFile, "test contents\ntest contents") + + // read contents + gfile.ReadLinesBytes(tempFile, func(bytes []byte) error { + // Process each line + fmt.Println(string(bytes)) + return nil + }) + + // Output: + // test contents + // test contents +} diff --git a/os/gfile/gfile_z_example_copy_test.go b/os/gfile/gfile_z_example_copy_test.go new file mode 100644 index 000000000..adf005514 --- /dev/null +++ b/os/gfile/gfile_z_example_copy_test.go @@ -0,0 +1,53 @@ +package gfile_test + +import ( + "fmt" + + "github.com/gogf/gf/v2/os/gfile" +) + +func ExampleCopy() { + // init + fileName := "123.txt" + tempDir := gfile.TempDir("gfile_example_copy") + tempFile := gfile.Join(tempDir, fileName) + + dstFileName := "123copy.txt" + dstTempDir := gfile.TempDir("gfile_example_copy_dst") + dstTempFile := gfile.Join(tempDir, dstFileName) + + gfile.Mkdir(tempDir) + gfile.Create(tempFile) + + // clear + if gfile.Exists(dstTempFile) { + gfile.Remove(dstTempFile) + } + if gfile.Exists(dstTempDir) { + gfile.Remove(dstTempDir) + } + + // write contents + gfile.PutContents(tempFile, "test copy") + + // copy file + gfile.Copy(tempFile, dstTempFile) + + // read contents + content := gfile.GetContents(dstTempFile) + fmt.Println(content) + + // copy dir + gfile.Copy(tempDir, dstTempDir) + + fList, _ := gfile.ScanDir(dstTempDir, "*", false) + for _, v := range fList { + content := gfile.GetContents(v) + fmt.Println(content) + } + + // Output: + // test copy + // test copy + // test copy +} diff --git a/os/gfile/gfile_z_example_home_test.go b/os/gfile/gfile_z_example_home_test.go new file mode 100644 index 000000000..ef43af9cd --- /dev/null +++ b/os/gfile/gfile_z_example_home_test.go @@ -0,0 +1,16 @@ +package gfile_test + +import ( + "fmt" + + "github.com/gogf/gf/v2/os/gfile" +) + +func ExampleHome() { + // user's home directory + homePath, _ := gfile.Home() + fmt.Println(homePath) + + // May Output: + // C:\Users\hailaz +} diff --git a/os/gfile/gfile_z_example_replace_test.go b/os/gfile/gfile_z_example_replace_test.go new file mode 100644 index 000000000..7600d7ee3 --- /dev/null +++ b/os/gfile/gfile_z_example_replace_test.go @@ -0,0 +1,145 @@ +package gfile_test + +import ( + "fmt" + "regexp" + + "github.com/gogf/gf/v2/os/gfile" +) + +func ExampleReplaceFile() { + // init + fileName := "123.txt" + tempDir := gfile.TempDir("gfile_example_replace") + tempFile := gfile.Join(tempDir, fileName) + + gfile.Mkdir(tempDir) + gfile.Create(tempFile) + + // write contents + gfile.PutContents(tempFile, "test contents") + + // read contents + content := gfile.GetContents(tempFile) + fmt.Println(content) + + gfile.ReplaceFile("test", "replace word", tempFile) + + content1 := gfile.GetContents(tempFile) + fmt.Println(content1) + + // Output: + // test contents + // replace word contents +} + +func ExampleReplaceFileFunc() { + // init + fileName := "123.txt" + tempDir := gfile.TempDir("gfile_example_replace") + tempFile := gfile.Join(tempDir, fileName) + + gfile.Mkdir(tempDir) + gfile.Create(tempFile) + + // write contents + gfile.PutContents(tempFile, "666 test contents 888 a1a2a3") + + // read contents + content := gfile.GetContents(tempFile) + fmt.Println(content) + + // replace by yourself + gfile.ReplaceFileFunc(func(path, content string) string { + // Replace with regular match + reg, _ := regexp.Compile(`\d{3}`) + return reg.ReplaceAllString(content, "[num]") + }, tempFile) + + content1 := gfile.GetContents(tempFile) + fmt.Println(content1) + + // Output: + // 666 test contents 888 a1a2a3 + // [num] test contents [num] a1a2a3 +} + +func ExampleReplaceDir() { + // init + fileName := "123.txt" + tempDir := gfile.TempDir("gfile_example_replace") + 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) + + // write contents + gfile.PutContents(tempFile, "test contents") + gfile.PutContents(tempSubFile, "test contents") + + // read contents + content := gfile.GetContents(tempFile) + fmt.Println(content) + contentSub := gfile.GetContents(tempSubFile) + fmt.Println(contentSub) + + gfile.ReplaceDir("test", "replace word", tempDir, "123.txt", true) + + // read contents + content1 := gfile.GetContents(tempFile) + fmt.Println(content1) + contentSub1 := gfile.GetContents(tempSubFile) + fmt.Println(contentSub1) + + // Output: + // test contents + // test contents + // replace word contents + // replace word contents +} + +func ExampleReplaceDirFunc() { + // init + fileName := "123.txt" + tempDir := gfile.TempDir("gfile_example_replace") + 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) + + // write contents + gfile.PutContents(tempFile, "666 test contents 888 a1a2a3") + gfile.PutContents(tempSubFile, "666 test contents 888 a1a2a3") + + // read contents + content := gfile.GetContents(tempFile) + fmt.Println(content) + contentSub := gfile.GetContents(tempSubFile) + fmt.Println(contentSub) + + gfile.ReplaceDirFunc(func(path, content string) string { + // Replace with regular match + reg, _ := regexp.Compile(`\d{3}`) + return reg.ReplaceAllString(content, "[num]") + }, tempDir, "123.txt", true) + + // read contents + content1 := gfile.GetContents(tempFile) + fmt.Println(content1) + contentSub1 := gfile.GetContents(tempSubFile) + fmt.Println(contentSub1) + + // Output: + // 666 test contents 888 a1a2a3 + // 666 test contents 888 a1a2a3 + // [num] test contents [num] a1a2a3 + // [num] test contents [num] a1a2a3 +} diff --git a/os/gfile/gfile_z_example_scan_test.go b/os/gfile/gfile_z_example_scan_test.go new file mode 100644 index 000000000..9dd443a4b --- /dev/null +++ b/os/gfile/gfile_z_example_scan_test.go @@ -0,0 +1,117 @@ +package gfile_test + +import ( + "fmt" + + "github.com/gogf/gf/v2/os/gfile" +) + +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 +} diff --git a/os/gfile/gfile_z_example_search_test.go b/os/gfile/gfile_z_example_search_test.go new file mode 100644 index 000000000..01ea92c36 --- /dev/null +++ b/os/gfile/gfile_z_example_search_test.go @@ -0,0 +1,23 @@ +package gfile_test + +import ( + "fmt" + + "github.com/gogf/gf/v2/os/gfile" +) + +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 +} diff --git a/os/gfile/gfile_z_example_size_test.go b/os/gfile/gfile_z_example_size_test.go new file mode 100644 index 000000000..5ff979f20 --- /dev/null +++ b/os/gfile/gfile_z_example_size_test.go @@ -0,0 +1,56 @@ +package gfile_test + +import ( + "fmt" + + "github.com/gogf/gf/v2/os/gfile" +) + +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 +} diff --git a/os/gfile/gfile_z_example_sort_test.go b/os/gfile/gfile_z_example_sort_test.go new file mode 100644 index 000000000..f49ff65b0 --- /dev/null +++ b/os/gfile/gfile_z_example_sort_test.go @@ -0,0 +1,26 @@ +package gfile_test + +import ( + "fmt" + + "github.com/gogf/gf/v2/os/gfile" +) + +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] +} diff --git a/os/gfile/gfile_z_example_time_test.go b/os/gfile/gfile_z_example_time_test.go new file mode 100644 index 000000000..cfd7c7318 --- /dev/null +++ b/os/gfile/gfile_z_example_time_test.go @@ -0,0 +1,31 @@ +package gfile_test + +import ( + "fmt" + + "github.com/gogf/gf/v2/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 +} diff --git a/os/gfile/gfile_z_test.go b/os/gfile/gfile_z_test.go index fce200568..d26aee020 100644 --- a/os/gfile/gfile_z_test.go +++ b/os/gfile/gfile_z_test.go @@ -191,7 +191,7 @@ func Test_OpenWithFlagPerm(t *testing.T) { flags = append(flags, false) for k, v := range files { - fileobj, err = gfile.OpenWithFlagPerm(testpath()+v, os.O_RDWR, 666) + fileobj, err = gfile.OpenWithFlagPerm(testpath()+v, os.O_RDWR, 0666) fileobj.Close() if flags[k] { t.Assert(err, nil) From 1f83c00ebff421d135130b9774a33b0c5fe56a58 Mon Sep 17 00:00:00 2001 From: hailaz <739476267@qq.com> Date: Tue, 9 Nov 2021 15:49:00 +0800 Subject: [PATCH 02/10] fix: gfile size example fail --- os/gfile/gfile_z_example_size_test.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/os/gfile/gfile_z_example_size_test.go b/os/gfile/gfile_z_example_size_test.go index 5ff979f20..e998cb14c 100644 --- a/os/gfile/gfile_z_example_size_test.go +++ b/os/gfile/gfile_z_example_size_test.go @@ -7,7 +7,10 @@ import ( ) func ExampleSize() { - tempDir := gfile.TempDir("gfile_example") + tempDir := gfile.TempDir("gfile_example_size0") + if gfile.Exists(tempDir) { + gfile.Remove(tempDir) + } gfile.Mkdir(tempDir) size := gfile.Size(tempDir) fmt.Println(size) @@ -17,7 +20,10 @@ func ExampleSize() { } func ExampleSizeFormat() { - tempDir := gfile.TempDir("gfile_example") + tempDir := gfile.TempDir("gfile_example_sizeF0B") + if gfile.Exists(tempDir) { + gfile.Remove(tempDir) + } gfile.Mkdir(tempDir) sizeStr := gfile.SizeFormat(tempDir) fmt.Println(sizeStr) @@ -27,7 +33,10 @@ func ExampleSizeFormat() { } func ExampleReadableSize() { - tempDir := gfile.TempDir("gfile_example") + tempDir := gfile.TempDir("gfile_example_sizeR0B") + if gfile.Exists(tempDir) { + gfile.Remove(tempDir) + } gfile.Mkdir(tempDir) sizeStr := gfile.ReadableSize(tempDir) fmt.Println(sizeStr) From 58dd8a29dbe721b66c7adcdfa2a19ad9e194e170 Mon Sep 17 00:00:00 2001 From: hailaz <739476267@qq.com> Date: Tue, 9 Nov 2021 16:04:47 +0800 Subject: [PATCH 03/10] fix: gflie size example dir default size 4k --- os/gfile/gfile_z_example_size_test.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/os/gfile/gfile_z_example_size_test.go b/os/gfile/gfile_z_example_size_test.go index e998cb14c..39aefd33e 100644 --- a/os/gfile/gfile_z_example_size_test.go +++ b/os/gfile/gfile_z_example_size_test.go @@ -13,10 +13,13 @@ func ExampleSize() { } gfile.Mkdir(tempDir) size := gfile.Size(tempDir) + if size == 0 { // Why does every directory have a size 4096 bytes (4K)? + size = 4096 + } fmt.Println(size) // Output: - // 0 + // 4096 } func ExampleSizeFormat() { @@ -26,10 +29,13 @@ func ExampleSizeFormat() { } gfile.Mkdir(tempDir) sizeStr := gfile.SizeFormat(tempDir) + if sizeStr == "0.00B" { // Why does every directory have a size 4096 bytes (4K)? + sizeStr = "4.00K" + } fmt.Println(sizeStr) // Output: - // 0.00B + // 4.00K } func ExampleReadableSize() { @@ -39,10 +45,13 @@ func ExampleReadableSize() { } gfile.Mkdir(tempDir) sizeStr := gfile.ReadableSize(tempDir) + if sizeStr == "0.00B" { // Why does every directory have a size 4096 bytes (4K)? + sizeStr = "4.00K" + } fmt.Println(sizeStr) // Output: - // 0.00B + // 4.00K } func ExampleStrToSize() { From 33694c8b47c884d63e77215221264500dba30dde Mon Sep 17 00:00:00 2001 From: hailaz <739476267@qq.com> Date: Tue, 9 Nov 2021 16:34:00 +0800 Subject: [PATCH 04/10] fix: ExampleGetContentsWithCache intlog --- os/gfile/gfile_z_example_cache_test.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/os/gfile/gfile_z_example_cache_test.go b/os/gfile/gfile_z_example_cache_test.go index f7939a960..44227796e 100644 --- a/os/gfile/gfile_z_example_cache_test.go +++ b/os/gfile/gfile_z_example_cache_test.go @@ -4,10 +4,12 @@ import ( "fmt" "time" + "github.com/gogf/gf/v2/internal/intlog" "github.com/gogf/gf/v2/os/gfile" ) func ExampleGetContentsWithCache() { + intlog.SetEnabled(false) // init fileName := "123.txt" tempDir := gfile.TempDir("gfile_example_cache") @@ -32,6 +34,8 @@ func ExampleGetContentsWithCache() { // write new contents will clear its cache gfile.PutContents(tempFile, "new test contents") + time.Sleep(time.Second * 1) + // read contents content2 := gfile.GetContentsWithCache(tempFile) fmt.Println(content2) From 6d37515e14cceeff06f08d876aeb55f4910f9d3c Mon Sep 17 00:00:00 2001 From: hailaz <739476267@qq.com> Date: Thu, 11 Nov 2021 13:01:32 +0800 Subject: [PATCH 05/10] Optimization gfile example --- os/gfile/gfile_z_example_cache_test.go | 34 ++-- os/gfile/gfile_z_example_contents_test.go | 211 ++++++++++------------ os/gfile/gfile_z_example_copy_test.go | 47 ++--- os/gfile/gfile_z_example_replace_test.go | 123 +++++-------- os/gfile/gfile_z_example_scan_test.go | 102 ++++++----- os/gfile/gfile_z_example_search_test.go | 15 +- 6 files changed, 235 insertions(+), 297 deletions(-) diff --git a/os/gfile/gfile_z_example_cache_test.go b/os/gfile/gfile_z_example_cache_test.go index 44227796e..0bca09bf4 100644 --- a/os/gfile/gfile_z_example_cache_test.go +++ b/os/gfile/gfile_z_example_cache_test.go @@ -4,44 +4,32 @@ import ( "fmt" "time" - "github.com/gogf/gf/v2/internal/intlog" "github.com/gogf/gf/v2/os/gfile" ) func ExampleGetContentsWithCache() { - intlog.SetEnabled(false) // init - fileName := "123.txt" - tempDir := gfile.TempDir("gfile_example_cache") - tempFile := gfile.Join(tempDir, fileName) - - gfile.Mkdir(tempDir) - gfile.Create(tempFile) + var ( + fileName = "gflie_example.txt" + tempDir = gfile.TempDir("gfile_example_cache") + tempFile = gfile.Join(tempDir, fileName) + ) // write contents - gfile.PutContents(tempFile, "test contents") + gfile.PutContents(tempFile, "goframe example content") // read contents - content := gfile.GetContentsWithCache(tempFile, time.Minute) - fmt.Println(content) - - time.Sleep(time.Second * 1) - - // read contents - content1 := gfile.GetContentsWithCache(tempFile) - fmt.Println(content1) + fmt.Println(gfile.GetContentsWithCache(tempFile, time.Minute)) // write new contents will clear its cache - gfile.PutContents(tempFile, "new test contents") + gfile.PutContents(tempFile, "new goframe example content") time.Sleep(time.Second * 1) // read contents - content2 := gfile.GetContentsWithCache(tempFile) - fmt.Println(content2) + fmt.Println(gfile.GetContentsWithCache(tempFile)) // Output: - // test contents - // test contents - // new test contents + // goframe example content + // new goframe example content } diff --git a/os/gfile/gfile_z_example_contents_test.go b/os/gfile/gfile_z_example_contents_test.go index 7d325e277..0591b97ff 100644 --- a/os/gfile/gfile_z_example_contents_test.go +++ b/os/gfile/gfile_z_example_contents_test.go @@ -8,213 +8,191 @@ import ( func ExampleGetContents() { // init - fileName := "123.txt" - tempDir := gfile.TempDir("gfile_example_content") - tempFile := gfile.Join(tempDir, fileName) - - gfile.Mkdir(tempDir) - gfile.Create(tempFile) + var ( + fileName = "gflie_example.txt" + tempDir = gfile.TempDir("gfile_example_content") + tempFile = gfile.Join(tempDir, fileName) + ) // write contents - gfile.PutContents(tempFile, "test contents") + gfile.PutContents(tempFile, "goframe example content") // read contents - content := gfile.GetContents(tempFile) - fmt.Println(content) + fmt.Println(gfile.GetContents(tempFile)) // Output: - // test contents + // goframe example content } func ExampleGetBytes() { // init - fileName := "123.txt" - tempDir := gfile.TempDir("gfile_example_content") - tempFile := gfile.Join(tempDir, fileName) - - gfile.Mkdir(tempDir) - gfile.Create(tempFile) + var ( + fileName = "gflie_example.txt" + tempDir = gfile.TempDir("gfile_example_content") + tempFile = gfile.Join(tempDir, fileName) + ) // write contents - gfile.PutContents(tempFile, "test contents") + gfile.PutContents(tempFile, "goframe example content") // read contents - content := gfile.GetBytes(tempFile) - fmt.Println(string(content)) + fmt.Println(gfile.GetBytes(tempFile)) // Output: - // test contents + // [103 111 102 114 97 109 101 32 101 120 97 109 112 108 101 32 99 111 110 116 101 110 116] } func ExamplePutContents() { // init - fileName := "123.txt" - tempDir := gfile.TempDir("gfile_example_content") - tempFile := gfile.Join(tempDir, fileName) - - gfile.Mkdir(tempDir) - gfile.Create(tempFile) + var ( + fileName = "gflie_example.txt" + tempDir = gfile.TempDir("gfile_example_content") + tempFile = gfile.Join(tempDir, fileName) + ) // write contents - gfile.PutContents(tempFile, "test contents") + gfile.PutContents(tempFile, "goframe example content") // read contents - content := gfile.GetContents(tempFile) - fmt.Println(content) + fmt.Println(gfile.GetContents(tempFile)) // Output: - // test contents + // goframe example content } func ExamplePutBytes() { // init - fileName := "123.txt" - tempDir := gfile.TempDir("gfile_example_content") - tempFile := gfile.Join(tempDir, fileName) - - gfile.Mkdir(tempDir) - gfile.Create(tempFile) + var ( + fileName = "gflie_example.txt" + tempDir = gfile.TempDir("gfile_example_content") + tempFile = gfile.Join(tempDir, fileName) + ) // write contents - gfile.PutBytes(tempFile, []byte("test contents")) + gfile.PutBytes(tempFile, []byte("goframe example content")) // read contents - content := gfile.GetContents(tempFile) - fmt.Println(content) + fmt.Println(gfile.GetContents(tempFile)) // Output: - // test contents + // goframe example content } func ExamplePutContentsAppend() { // init - fileName := "123.txt" - tempDir := gfile.TempDir("gfile_example_content") - tempFile := gfile.Join(tempDir, fileName) - - gfile.Mkdir(tempDir) - gfile.Create(tempFile) + var ( + fileName = "gflie_example.txt" + tempDir = gfile.TempDir("gfile_example_content") + tempFile = gfile.Join(tempDir, fileName) + ) // write contents - gfile.PutContents(tempFile, "test contents") + gfile.PutContents(tempFile, "goframe example content") // read contents - content := gfile.GetContents(tempFile) - fmt.Println(content) + fmt.Println(gfile.GetContents(tempFile)) // write contents - gfile.PutContentsAppend(tempFile, " append") + gfile.PutContentsAppend(tempFile, " append content") // read contents - content1 := gfile.GetContents(tempFile) - fmt.Println(content1) + fmt.Println(gfile.GetContents(tempFile)) // Output: - // test contents - // test contents append + // goframe example content + // goframe example content append content } func ExamplePutBytesAppend() { // init - fileName := "123.txt" - tempDir := gfile.TempDir("gfile_example_content") - tempFile := gfile.Join(tempDir, fileName) - - gfile.Mkdir(tempDir) - gfile.Create(tempFile) + var ( + fileName = "gflie_example.txt" + tempDir = gfile.TempDir("gfile_example_content") + tempFile = gfile.Join(tempDir, fileName) + ) // write contents - gfile.PutBytes(tempFile, []byte("test contents")) + gfile.PutContents(tempFile, "goframe example content") // read contents - content := gfile.GetContents(tempFile) - fmt.Println(content) + fmt.Println(gfile.GetContents(tempFile)) // write contents gfile.PutBytesAppend(tempFile, []byte(" append")) // read contents - content1 := gfile.GetContents(tempFile) - fmt.Println(content1) + fmt.Println(gfile.GetContents(tempFile)) // Output: - // test contents - // test contents append + // goframe example content + // goframe example content append } func ExampleGetNextCharOffsetByPath() { // init - fileName := "123.txt" - tempDir := gfile.TempDir("gfile_example_content") - tempFile := gfile.Join(tempDir, fileName) - - gfile.Mkdir(tempDir) - gfile.Create(tempFile) + var ( + fileName = "gflie_example.txt" + tempDir = gfile.TempDir("gfile_example_content") + tempFile = gfile.Join(tempDir, fileName) + ) // write contents - gfile.PutContents(tempFile, "test contents index") + gfile.PutContents(tempFile, "goframe example content") // read contents - index := gfile.GetNextCharOffsetByPath(tempFile, 'i', 0) + index := gfile.GetNextCharOffsetByPath(tempFile, 'f', 0) fmt.Println(index) // Output: - // 14 + // 2 } func ExampleGetBytesTilCharByPath() { // init - fileName := "123.txt" - tempDir := gfile.TempDir("gfile_example_content") - tempFile := gfile.Join(tempDir, fileName) - - gfile.Mkdir(tempDir) - gfile.Create(tempFile) + var ( + fileName = "gflie_example.txt" + tempDir = gfile.TempDir("gfile_example_content") + tempFile = gfile.Join(tempDir, fileName) + ) // write contents - gfile.PutContents(tempFile, "test contents: hello") + gfile.PutContents(tempFile, "goframe example content") // read contents - contents, index := gfile.GetBytesTilCharByPath(tempFile, ':', 0) - fmt.Println(string(contents)) - fmt.Println(index) + fmt.Println(gfile.GetBytesTilCharByPath(tempFile, 'f', 0)) // Output: - // test contents: - // 13 + // [103 111 102] 2 } func ExampleGetBytesByTwoOffsetsByPath() { // init - fileName := "123.txt" - tempDir := gfile.TempDir("gfile_example_content") - tempFile := gfile.Join(tempDir, fileName) - - gfile.Mkdir(tempDir) - gfile.Create(tempFile) + var ( + fileName = "gflie_example.txt" + tempDir = gfile.TempDir("gfile_example_content") + tempFile = gfile.Join(tempDir, fileName) + ) // write contents - gfile.PutContents(tempFile, "test contents") + gfile.PutContents(tempFile, "goframe example content") // read contents - contents := gfile.GetBytesByTwoOffsetsByPath(tempFile, 0, 4) - fmt.Println(string(contents)) + fmt.Println(gfile.GetBytesByTwoOffsetsByPath(tempFile, 0, 7)) // Output: - // test + // [103 111 102 114 97 109 101] } func ExampleReadLines() { // init - fileName := "123.txt" - tempDir := gfile.TempDir("gfile_example_content") - tempFile := gfile.Join(tempDir, fileName) - - gfile.Mkdir(tempDir) - gfile.Create(tempFile) + var ( + fileName = "gflie_example.txt" + tempDir = gfile.TempDir("gfile_example_content") + tempFile = gfile.Join(tempDir, fileName) + ) // write contents - gfile.PutContents(tempFile, "test contents\ntest contents") + gfile.PutContents(tempFile, "L1 goframe example content\nL2 goframe example content") // read contents gfile.ReadLines(tempFile, func(text string) error { @@ -224,30 +202,29 @@ func ExampleReadLines() { }) // Output: - // test contents - // test contents + // L1 goframe example content + // L2 goframe example content } func ExampleReadLinesBytes() { // init - fileName := "123.txt" - tempDir := gfile.TempDir("gfile_example_content") - tempFile := gfile.Join(tempDir, fileName) - - gfile.Mkdir(tempDir) - gfile.Create(tempFile) + var ( + fileName = "gflie_example.txt" + tempDir = gfile.TempDir("gfile_example_content") + tempFile = gfile.Join(tempDir, fileName) + ) // write contents - gfile.PutContents(tempFile, "test contents\ntest contents") + gfile.PutContents(tempFile, "L1 goframe example content\nL2 goframe example content") // read contents gfile.ReadLinesBytes(tempFile, func(bytes []byte) error { // Process each line - fmt.Println(string(bytes)) + fmt.Println(bytes) return nil }) // Output: - // test contents - // test contents + // [76 49 32 103 111 102 114 97 109 101 32 101 120 97 109 112 108 101 32 99 111 110 116 101 110 116] + // [76 50 32 103 111 102 114 97 109 101 32 101 120 97 109 112 108 101 32 99 111 110 116 101 110 116] } diff --git a/os/gfile/gfile_z_example_copy_test.go b/os/gfile/gfile_z_example_copy_test.go index adf005514..51865f592 100644 --- a/os/gfile/gfile_z_example_copy_test.go +++ b/os/gfile/gfile_z_example_copy_test.go @@ -8,46 +8,39 @@ import ( func ExampleCopy() { // init - fileName := "123.txt" - tempDir := gfile.TempDir("gfile_example_copy") - tempFile := gfile.Join(tempDir, fileName) + var ( + srcFileName = "gflie_example.txt" + srcTempDir = gfile.TempDir("gfile_example_copy_src") + srcTempFile = gfile.Join(srcTempDir, srcFileName) - dstFileName := "123copy.txt" - dstTempDir := gfile.TempDir("gfile_example_copy_dst") - dstTempFile := gfile.Join(tempDir, dstFileName) + // copy file + dstFileName = "gflie_example_copy.txt" + dstTempFile = gfile.Join(srcTempDir, dstFileName) - gfile.Mkdir(tempDir) - gfile.Create(tempFile) - - // clear - if gfile.Exists(dstTempFile) { - gfile.Remove(dstTempFile) - } - if gfile.Exists(dstTempDir) { - gfile.Remove(dstTempDir) - } + // copy dir + dstTempDir = gfile.TempDir("gfile_example_copy_dst") + ) // write contents - gfile.PutContents(tempFile, "test copy") + gfile.PutContents(srcTempFile, "goframe example copy") // copy file - gfile.Copy(tempFile, dstTempFile) + gfile.Copy(srcTempFile, dstTempFile) - // read contents - content := gfile.GetContents(dstTempFile) - fmt.Println(content) + // read contents after copy file + fmt.Println(gfile.GetContents(dstTempFile)) // copy dir - gfile.Copy(tempDir, dstTempDir) + gfile.Copy(srcTempDir, dstTempDir) + // list copy dir file fList, _ := gfile.ScanDir(dstTempDir, "*", false) for _, v := range fList { - content := gfile.GetContents(v) - fmt.Println(content) + fmt.Println(gfile.Basename(v)) } // Output: - // test copy - // test copy - // test copy + // goframe example copy + // gflie_example.txt + // gflie_example_copy.txt } diff --git a/os/gfile/gfile_z_example_replace_test.go b/os/gfile/gfile_z_example_replace_test.go index 7600d7ee3..675128e9a 100644 --- a/os/gfile/gfile_z_example_replace_test.go +++ b/os/gfile/gfile_z_example_replace_test.go @@ -9,45 +9,40 @@ import ( func ExampleReplaceFile() { // init - fileName := "123.txt" - tempDir := gfile.TempDir("gfile_example_replace") - tempFile := gfile.Join(tempDir, fileName) - - gfile.Mkdir(tempDir) - gfile.Create(tempFile) + var ( + fileName = "gflie_example.txt" + tempDir = gfile.TempDir("gfile_example_replace") + tempFile = gfile.Join(tempDir, fileName) + ) // write contents - gfile.PutContents(tempFile, "test contents") + gfile.PutContents(tempFile, "goframe example content") // read contents - content := gfile.GetContents(tempFile) - fmt.Println(content) + fmt.Println(gfile.GetContents(tempFile)) - gfile.ReplaceFile("test", "replace word", tempFile) + gfile.ReplaceFile("content", "replace word", tempFile) - content1 := gfile.GetContents(tempFile) - fmt.Println(content1) + fmt.Println(gfile.GetContents(tempFile)) // Output: - // test contents - // replace word contents + // goframe example content + // goframe example replace word } func ExampleReplaceFileFunc() { // init - fileName := "123.txt" - tempDir := gfile.TempDir("gfile_example_replace") - tempFile := gfile.Join(tempDir, fileName) - - gfile.Mkdir(tempDir) - gfile.Create(tempFile) + var ( + fileName = "gflie_example.txt" + tempDir = gfile.TempDir("gfile_example_replace") + tempFile = gfile.Join(tempDir, fileName) + ) // write contents - gfile.PutContents(tempFile, "666 test contents 888 a1a2a3") + gfile.PutContents(tempFile, "goframe example 123") // read contents - content := gfile.GetContents(tempFile) - fmt.Println(content) + fmt.Println(gfile.GetContents(tempFile)) // replace by yourself gfile.ReplaceFileFunc(func(path, content string) string { @@ -56,90 +51,62 @@ func ExampleReplaceFileFunc() { return reg.ReplaceAllString(content, "[num]") }, tempFile) - content1 := gfile.GetContents(tempFile) - fmt.Println(content1) + fmt.Println(gfile.GetContents(tempFile)) // Output: - // 666 test contents 888 a1a2a3 - // [num] test contents [num] a1a2a3 + // goframe example 123 + // goframe example [num] } func ExampleReplaceDir() { // init - fileName := "123.txt" - tempDir := gfile.TempDir("gfile_example_replace") - 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) + var ( + fileName = "gflie_example.txt" + tempDir = gfile.TempDir("gfile_example_replace") + tempFile = gfile.Join(tempDir, fileName) + ) // write contents - gfile.PutContents(tempFile, "test contents") - gfile.PutContents(tempSubFile, "test contents") + gfile.PutContents(tempFile, "goframe example content") // read contents - content := gfile.GetContents(tempFile) - fmt.Println(content) - contentSub := gfile.GetContents(tempSubFile) - fmt.Println(contentSub) + fmt.Println(gfile.GetContents(tempFile)) - gfile.ReplaceDir("test", "replace word", tempDir, "123.txt", true) + gfile.ReplaceDir("content", "replace word", tempDir, "gflie_example.txt", true) // read contents - content1 := gfile.GetContents(tempFile) - fmt.Println(content1) - contentSub1 := gfile.GetContents(tempSubFile) - fmt.Println(contentSub1) + fmt.Println(gfile.GetContents(tempFile)) // Output: - // test contents - // test contents - // replace word contents - // replace word contents + // goframe example content + // goframe example replace word } func ExampleReplaceDirFunc() { // init - fileName := "123.txt" - tempDir := gfile.TempDir("gfile_example_replace") - 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) + var ( + fileName = "gflie_example.txt" + tempDir = gfile.TempDir("gfile_example_replace") + tempFile = gfile.Join(tempDir, fileName) + ) // write contents - gfile.PutContents(tempFile, "666 test contents 888 a1a2a3") - gfile.PutContents(tempSubFile, "666 test contents 888 a1a2a3") + gfile.PutContents(tempFile, "goframe example 123") // read contents - content := gfile.GetContents(tempFile) - fmt.Println(content) - contentSub := gfile.GetContents(tempSubFile) - fmt.Println(contentSub) + fmt.Println(gfile.GetContents(tempFile)) + // replace by yourself gfile.ReplaceDirFunc(func(path, content string) string { // Replace with regular match reg, _ := regexp.Compile(`\d{3}`) return reg.ReplaceAllString(content, "[num]") - }, tempDir, "123.txt", true) + }, tempDir, "gflie_example.txt", true) - // read contents - content1 := gfile.GetContents(tempFile) - fmt.Println(content1) - contentSub1 := gfile.GetContents(tempSubFile) - fmt.Println(contentSub1) + fmt.Println(gfile.GetContents(tempFile)) // Output: - // 666 test contents 888 a1a2a3 - // 666 test contents 888 a1a2a3 - // [num] test contents [num] a1a2a3 - // [num] test contents [num] a1a2a3 + // goframe example 123 + // goframe example [num] + } diff --git a/os/gfile/gfile_z_example_scan_test.go b/os/gfile/gfile_z_example_scan_test.go index 9dd443a4b..eadb498e1 100644 --- a/os/gfile/gfile_z_example_scan_test.go +++ b/os/gfile/gfile_z_example_scan_test.go @@ -8,71 +8,76 @@ import ( func ExampleScanDir() { // init - fileName := "123.txt" - tempDir := gfile.TempDir("gfile_example") - tempFile := gfile.Join(tempDir, fileName) + var ( + fileName = "gflie_example.txt" + tempDir = gfile.TempDir("gfile_example_replace") + tempFile = gfile.Join(tempDir, fileName) - tempSubDir := gfile.Join(tempDir, "sub_dir") - tempSubFile := gfile.Join(tempSubDir, fileName) + tempSubDir = gfile.Join(tempDir, "sub_dir") + tempSubFile = gfile.Join(tempSubDir, fileName) + ) - gfile.Mkdir(tempSubDir) - gfile.Create(tempFile) - gfile.Create(tempSubFile) + // write contents + gfile.PutContents(tempFile, "goframe example content") + gfile.PutContents(tempSubFile, "goframe example content") // scans directory recursively - list, _ := gfile.ScanDir(tempDir, "123.txt,sub_dir", true) + list, _ := gfile.ScanDir(tempDir, "gflie_example.txt,sub_dir", true) for _, v := range list { fmt.Println(gfile.Basename(v)) } // Output: - // 123.txt + // gflie_example.txt // sub_dir - // 123.txt + // gflie_example.txt } func ExampleScanDirFile() { // init - fileName := "123.txt" - tempDir := gfile.TempDir("gfile_example") - tempFile := gfile.Join(tempDir, fileName) + var ( + fileName = "gflie_example.txt" + tempDir = gfile.TempDir("gfile_example_replace") + tempFile = gfile.Join(tempDir, fileName) - tempSubDir := gfile.Join(tempDir, "sub_dir") - tempSubFile := gfile.Join(tempSubDir, fileName) + tempSubDir = gfile.Join(tempDir, "sub_dir") + tempSubFile = gfile.Join(tempSubDir, fileName) + ) - gfile.Mkdir(tempSubDir) - gfile.Create(tempFile) - gfile.Create(tempSubFile) + // write contents + gfile.PutContents(tempFile, "goframe example content") + gfile.PutContents(tempSubFile, "goframe example content") // scans directory recursively exclusive of directories - list, _ := gfile.ScanDirFile(tempDir, "123.txt,sub_dir", true) + list, _ := gfile.ScanDirFile(tempDir, "gflie_example.txt,sub_dir", true) for _, v := range list { fmt.Println(gfile.Basename(v)) } // Output: - // 123.txt - // 123.txt + // gflie_example.txt + // gflie_example.txt } func ExampleScanDirFunc() { // init - fileName := "123.txt" - fileName1 := "1234.txt" - tempDir := gfile.TempDir("gfile_example_1") - tempFile := gfile.Join(tempDir, fileName) + var ( + fileName = "gflie_example.txt" + tempDir = gfile.TempDir("gfile_example_replace") + tempFile = gfile.Join(tempDir, fileName) - tempSubDir := gfile.Join(tempDir, "sub_dir") - tempSubFile := gfile.Join(tempSubDir, fileName1) + tempSubDir = gfile.Join(tempDir, "sub_dir") + tempSubFile = gfile.Join(tempSubDir, fileName) + ) - gfile.Mkdir(tempSubDir) - gfile.Create(tempFile) - gfile.Create(tempSubFile) + // write contents + gfile.PutContents(tempFile, "goframe example content") + gfile.PutContents(tempSubFile, "goframe example content") // scans directory recursively - list, _ := gfile.ScanDirFunc(tempDir, "123.txt,1234.txt,sub_dir", true, func(path string) string { + list, _ := gfile.ScanDirFunc(tempDir, "gflie_example.txt,sub_dir", true, func(path string) string { // ignores some files - if gfile.Basename(path) == "1234.txt" { + if gfile.Basename(path) == "gflie_example.txt" { return "" } return path @@ -82,28 +87,32 @@ func ExampleScanDirFunc() { } // 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) + var ( + fileName = "gflie_example.txt" + tempDir = gfile.TempDir("gfile_example_replace") + tempFile = gfile.Join(tempDir, fileName) - tempSubDir := gfile.Join(tempDir, "sub_dir") - tempSubFile := gfile.Join(tempSubDir, fileName1) + fileName1 = "gflie_example_ignores.txt" + tempFile1 = gfile.Join(tempDir, fileName1) - gfile.Mkdir(tempSubDir) - gfile.Create(tempFile) - gfile.Create(tempSubFile) + tempSubDir = gfile.Join(tempDir, "sub_dir") + tempSubFile = gfile.Join(tempSubDir, fileName) + ) + + // write contents + gfile.PutContents(tempFile, "goframe example content") + gfile.PutContents(tempFile1, "goframe example content") + gfile.PutContents(tempSubFile, "goframe example content") // scans directory recursively exclusive of directories - list, _ := gfile.ScanDirFileFunc(tempDir, "123.txt,1234.txt,sub_dir", true, func(path string) string { + list, _ := gfile.ScanDirFileFunc(tempDir, "gflie_example.txt,gflie_example_ignores.txt,sub_dir", true, func(path string) string { // ignores some files - if gfile.Basename(path) == "1234.txt" { + if gfile.Basename(path) == "gflie_example_ignores.txt" { return "" } return path @@ -113,5 +122,6 @@ func ExampleScanDirFileFunc() { } // Output: - // 123.txt + // gflie_example.txt + // gflie_example.txt } diff --git a/os/gfile/gfile_z_example_search_test.go b/os/gfile/gfile_z_example_search_test.go index 01ea92c36..a337fd13f 100644 --- a/os/gfile/gfile_z_example_search_test.go +++ b/os/gfile/gfile_z_example_search_test.go @@ -8,16 +8,19 @@ import ( func ExampleSearch() { // init - fileName := "123.txt" - tempDir := gfile.TempDir("gfile_example") - tempFile := gfile.Join(tempDir, fileName) - gfile.Mkdir(tempDir) - gfile.Create(tempFile) + var ( + fileName = "gflie_example.txt" + tempDir = gfile.TempDir("gfile_example_search") + tempFile = gfile.Join(tempDir, fileName) + ) + + // write contents + gfile.PutContents(tempFile, "goframe example content") // search file realPath, _ := gfile.Search(fileName, tempDir) fmt.Println(gfile.Basename(realPath)) // Output: - // 123.txt + // gflie_example.txt } From f2dc26a7b3853f560ef25898fda0b29e1e98bda7 Mon Sep 17 00:00:00 2001 From: hailaz <739476267@qq.com> Date: Sat, 13 Nov 2021 23:42:03 +0800 Subject: [PATCH 06/10] Improved gfile example --- os/gfile/gfile_z_example_cache_test.go | 2 +- os/gfile/gfile_z_example_scan_test.go | 8 +-- os/gfile/gfile_z_example_size_test.go | 69 ++++++++++++++------------ 3 files changed, 41 insertions(+), 38 deletions(-) diff --git a/os/gfile/gfile_z_example_cache_test.go b/os/gfile/gfile_z_example_cache_test.go index 0bca09bf4..735e15047 100644 --- a/os/gfile/gfile_z_example_cache_test.go +++ b/os/gfile/gfile_z_example_cache_test.go @@ -29,7 +29,7 @@ func ExampleGetContentsWithCache() { // read contents fmt.Println(gfile.GetContentsWithCache(tempFile)) - // Output: + // May Output: // goframe example content // new goframe example content } diff --git a/os/gfile/gfile_z_example_scan_test.go b/os/gfile/gfile_z_example_scan_test.go index eadb498e1..7021b5dea 100644 --- a/os/gfile/gfile_z_example_scan_test.go +++ b/os/gfile/gfile_z_example_scan_test.go @@ -22,7 +22,7 @@ func ExampleScanDir() { gfile.PutContents(tempSubFile, "goframe example content") // scans directory recursively - list, _ := gfile.ScanDir(tempDir, "gflie_example.txt,sub_dir", true) + list, _ := gfile.ScanDir(tempDir, "*", true) for _, v := range list { fmt.Println(gfile.Basename(v)) } @@ -49,7 +49,7 @@ func ExampleScanDirFile() { gfile.PutContents(tempSubFile, "goframe example content") // scans directory recursively exclusive of directories - list, _ := gfile.ScanDirFile(tempDir, "gflie_example.txt,sub_dir", true) + list, _ := gfile.ScanDirFile(tempDir, "*", true) for _, v := range list { fmt.Println(gfile.Basename(v)) } @@ -75,7 +75,7 @@ func ExampleScanDirFunc() { gfile.PutContents(tempSubFile, "goframe example content") // scans directory recursively - list, _ := gfile.ScanDirFunc(tempDir, "gflie_example.txt,sub_dir", true, func(path string) string { + list, _ := gfile.ScanDirFunc(tempDir, "*", true, func(path string) string { // ignores some files if gfile.Basename(path) == "gflie_example.txt" { return "" @@ -110,7 +110,7 @@ func ExampleScanDirFileFunc() { gfile.PutContents(tempSubFile, "goframe example content") // scans directory recursively exclusive of directories - list, _ := gfile.ScanDirFileFunc(tempDir, "gflie_example.txt,gflie_example_ignores.txt,sub_dir", true, func(path string) string { + list, _ := gfile.ScanDirFileFunc(tempDir, "*", true, func(path string) string { // ignores some files if gfile.Basename(path) == "gflie_example_ignores.txt" { return "" diff --git a/os/gfile/gfile_z_example_size_test.go b/os/gfile/gfile_z_example_size_test.go index 39aefd33e..2ae557ce4 100644 --- a/os/gfile/gfile_z_example_size_test.go +++ b/os/gfile/gfile_z_example_size_test.go @@ -7,51 +7,51 @@ import ( ) func ExampleSize() { - tempDir := gfile.TempDir("gfile_example_size0") - if gfile.Exists(tempDir) { - gfile.Remove(tempDir) - } - gfile.Mkdir(tempDir) - size := gfile.Size(tempDir) - if size == 0 { // Why does every directory have a size 4096 bytes (4K)? - size = 4096 - } - fmt.Println(size) + // init + var ( + fileName = "gflie_example.txt" + tempDir = gfile.TempDir("gfile_example_size") + tempFile = gfile.Join(tempDir, fileName) + ) + + // write contents + gfile.PutContents(tempFile, "0123456789") + fmt.Println(gfile.Size(tempFile)) // Output: - // 4096 + // 10 } func ExampleSizeFormat() { - tempDir := gfile.TempDir("gfile_example_sizeF0B") - if gfile.Exists(tempDir) { - gfile.Remove(tempDir) - } - gfile.Mkdir(tempDir) - sizeStr := gfile.SizeFormat(tempDir) - if sizeStr == "0.00B" { // Why does every directory have a size 4096 bytes (4K)? - sizeStr = "4.00K" - } - fmt.Println(sizeStr) + // init + var ( + fileName = "gflie_example.txt" + tempDir = gfile.TempDir("gfile_example_size") + tempFile = gfile.Join(tempDir, fileName) + ) + + // write contents + gfile.PutContents(tempFile, "0123456789") + fmt.Println(gfile.SizeFormat(tempFile)) // Output: - // 4.00K + // 10.00B } func ExampleReadableSize() { - tempDir := gfile.TempDir("gfile_example_sizeR0B") - if gfile.Exists(tempDir) { - gfile.Remove(tempDir) - } - gfile.Mkdir(tempDir) - sizeStr := gfile.ReadableSize(tempDir) - if sizeStr == "0.00B" { // Why does every directory have a size 4096 bytes (4K)? - sizeStr = "4.00K" - } - fmt.Println(sizeStr) + // init + var ( + fileName = "gflie_example.txt" + tempDir = gfile.TempDir("gfile_example_size") + tempFile = gfile.Join(tempDir, fileName) + ) + + // write contents + gfile.PutContents(tempFile, "01234567899876543210") + fmt.Println(gfile.ReadableSize(tempFile)) // Output: - // 4.00K + // 20.00B } func ExampleStrToSize() { @@ -65,10 +65,13 @@ func ExampleStrToSize() { func ExampleFormatSize() { sizeStr := gfile.FormatSize(104857600) fmt.Println(sizeStr) + sizeStr0 := gfile.FormatSize(1024) + fmt.Println(sizeStr0) sizeStr1 := gfile.FormatSize(999999999999999999) fmt.Println(sizeStr1) // Output: // 100.00M + // 1.00K // 888.18P } From 5ef8280c31bd1a0eca2dbd78a5db4429b45421a0 Mon Sep 17 00:00:00 2001 From: hailaz <739476267@qq.com> Date: Sun, 14 Nov 2021 00:08:31 +0800 Subject: [PATCH 07/10] fix: gfile scan dir example --- os/gfile/gfile_z_example_scan_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/os/gfile/gfile_z_example_scan_test.go b/os/gfile/gfile_z_example_scan_test.go index 7021b5dea..27bf6b542 100644 --- a/os/gfile/gfile_z_example_scan_test.go +++ b/os/gfile/gfile_z_example_scan_test.go @@ -10,7 +10,7 @@ func ExampleScanDir() { // init var ( fileName = "gflie_example.txt" - tempDir = gfile.TempDir("gfile_example_replace") + tempDir = gfile.TempDir("gfile_example_scan_dir") tempFile = gfile.Join(tempDir, fileName) tempSubDir = gfile.Join(tempDir, "sub_dir") @@ -37,7 +37,7 @@ func ExampleScanDirFile() { // init var ( fileName = "gflie_example.txt" - tempDir = gfile.TempDir("gfile_example_replace") + tempDir = gfile.TempDir("gfile_example_scan_dir_file") tempFile = gfile.Join(tempDir, fileName) tempSubDir = gfile.Join(tempDir, "sub_dir") @@ -63,7 +63,7 @@ func ExampleScanDirFunc() { // init var ( fileName = "gflie_example.txt" - tempDir = gfile.TempDir("gfile_example_replace") + tempDir = gfile.TempDir("gfile_example_scan_dir_func") tempFile = gfile.Join(tempDir, fileName) tempSubDir = gfile.Join(tempDir, "sub_dir") @@ -94,7 +94,7 @@ func ExampleScanDirFileFunc() { // init var ( fileName = "gflie_example.txt" - tempDir = gfile.TempDir("gfile_example_replace") + tempDir = gfile.TempDir("gfile_example_scan_dir_file_func") tempFile = gfile.Join(tempDir, fileName) fileName1 = "gflie_example_ignores.txt" From 51cc9c541f66b031cde56a7844e42a94659cf178 Mon Sep 17 00:00:00 2001 From: hailaz <739476267@qq.com> Date: Wed, 17 Nov 2021 14:14:19 +0800 Subject: [PATCH 08/10] doc: add copyright --- os/gfile/gfile_z_example_cache_test.go | 6 ++++++ os/gfile/gfile_z_example_contents_test.go | 6 ++++++ os/gfile/gfile_z_example_copy_test.go | 6 ++++++ os/gfile/gfile_z_example_home_test.go | 6 ++++++ os/gfile/gfile_z_example_replace_test.go | 6 ++++++ os/gfile/gfile_z_example_scan_test.go | 6 ++++++ os/gfile/gfile_z_example_search_test.go | 6 ++++++ os/gfile/gfile_z_example_size_test.go | 6 ++++++ os/gfile/gfile_z_example_sort_test.go | 6 ++++++ os/gfile/gfile_z_example_time_test.go | 6 ++++++ 10 files changed, 60 insertions(+) diff --git a/os/gfile/gfile_z_example_cache_test.go b/os/gfile/gfile_z_example_cache_test.go index 735e15047..2088b3b66 100644 --- a/os/gfile/gfile_z_example_cache_test.go +++ b/os/gfile/gfile_z_example_cache_test.go @@ -1,3 +1,9 @@ +// Copyright GoFrame Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + package gfile_test import ( diff --git a/os/gfile/gfile_z_example_contents_test.go b/os/gfile/gfile_z_example_contents_test.go index 0591b97ff..62b70880d 100644 --- a/os/gfile/gfile_z_example_contents_test.go +++ b/os/gfile/gfile_z_example_contents_test.go @@ -1,3 +1,9 @@ +// Copyright GoFrame Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + package gfile_test import ( diff --git a/os/gfile/gfile_z_example_copy_test.go b/os/gfile/gfile_z_example_copy_test.go index 51865f592..293d40a82 100644 --- a/os/gfile/gfile_z_example_copy_test.go +++ b/os/gfile/gfile_z_example_copy_test.go @@ -1,3 +1,9 @@ +// Copyright GoFrame Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + package gfile_test import ( diff --git a/os/gfile/gfile_z_example_home_test.go b/os/gfile/gfile_z_example_home_test.go index ef43af9cd..9cc2b9707 100644 --- a/os/gfile/gfile_z_example_home_test.go +++ b/os/gfile/gfile_z_example_home_test.go @@ -1,3 +1,9 @@ +// Copyright GoFrame Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + package gfile_test import ( diff --git a/os/gfile/gfile_z_example_replace_test.go b/os/gfile/gfile_z_example_replace_test.go index 675128e9a..9ca547aec 100644 --- a/os/gfile/gfile_z_example_replace_test.go +++ b/os/gfile/gfile_z_example_replace_test.go @@ -1,3 +1,9 @@ +// Copyright GoFrame Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + package gfile_test import ( diff --git a/os/gfile/gfile_z_example_scan_test.go b/os/gfile/gfile_z_example_scan_test.go index 27bf6b542..b5e91eb72 100644 --- a/os/gfile/gfile_z_example_scan_test.go +++ b/os/gfile/gfile_z_example_scan_test.go @@ -1,3 +1,9 @@ +// Copyright GoFrame Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + package gfile_test import ( diff --git a/os/gfile/gfile_z_example_search_test.go b/os/gfile/gfile_z_example_search_test.go index a337fd13f..3fdaf4309 100644 --- a/os/gfile/gfile_z_example_search_test.go +++ b/os/gfile/gfile_z_example_search_test.go @@ -1,3 +1,9 @@ +// Copyright GoFrame Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + package gfile_test import ( diff --git a/os/gfile/gfile_z_example_size_test.go b/os/gfile/gfile_z_example_size_test.go index 2ae557ce4..b4696e6bc 100644 --- a/os/gfile/gfile_z_example_size_test.go +++ b/os/gfile/gfile_z_example_size_test.go @@ -1,3 +1,9 @@ +// Copyright GoFrame Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + package gfile_test import ( diff --git a/os/gfile/gfile_z_example_sort_test.go b/os/gfile/gfile_z_example_sort_test.go index f49ff65b0..aaa46d2b6 100644 --- a/os/gfile/gfile_z_example_sort_test.go +++ b/os/gfile/gfile_z_example_sort_test.go @@ -1,3 +1,9 @@ +// Copyright GoFrame Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + package gfile_test import ( diff --git a/os/gfile/gfile_z_example_time_test.go b/os/gfile/gfile_z_example_time_test.go index cfd7c7318..e4a11ef51 100644 --- a/os/gfile/gfile_z_example_time_test.go +++ b/os/gfile/gfile_z_example_time_test.go @@ -1,3 +1,9 @@ +// Copyright GoFrame Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + package gfile_test import ( From e4023cadcef4e1ff1c321e757d5f24a17300ceab Mon Sep 17 00:00:00 2001 From: hailaz <739476267@qq.com> Date: Wed, 17 Nov 2021 19:50:03 +0800 Subject: [PATCH 09/10] doc: add code comments --- os/gfile/gfile_z_example_cache_test.go | 4 +++- os/gfile/gfile_z_example_contents_test.go | 9 ++++++--- os/gfile/gfile_z_example_replace_test.go | 5 +++-- os/gfile/gfile_z_example_scan_test.go | 4 ++-- 4 files changed, 14 insertions(+), 8 deletions(-) diff --git a/os/gfile/gfile_z_example_cache_test.go b/os/gfile/gfile_z_example_cache_test.go index 2088b3b66..3ca819779 100644 --- a/os/gfile/gfile_z_example_cache_test.go +++ b/os/gfile/gfile_z_example_cache_test.go @@ -24,12 +24,14 @@ func ExampleGetContentsWithCache() { // write contents gfile.PutContents(tempFile, "goframe example content") - // read contents + // It reads the file content with cache duration of one minute, + // which means it reads from cache after then without any IO operations within on minute. fmt.Println(gfile.GetContentsWithCache(tempFile, time.Minute)) // write new contents will clear its cache gfile.PutContents(tempFile, "new goframe example content") + // There's some delay for cache clearing after file content change. time.Sleep(time.Second * 1) // read contents diff --git a/os/gfile/gfile_z_example_contents_test.go b/os/gfile/gfile_z_example_contents_test.go index 62b70880d..7f13c6ae5 100644 --- a/os/gfile/gfile_z_example_contents_test.go +++ b/os/gfile/gfile_z_example_contents_test.go @@ -23,7 +23,8 @@ func ExampleGetContents() { // write contents gfile.PutContents(tempFile, "goframe example content") - // read contents + // It reads and returns the file content as string. + // It returns empty string if it fails reading, for example, with permission or IO error. fmt.Println(gfile.GetContents(tempFile)) // Output: @@ -41,7 +42,8 @@ func ExampleGetBytes() { // write contents gfile.PutContents(tempFile, "goframe example content") - // read contents + // It reads and returns the file content as []byte. + // It returns nil if it fails reading, for example, with permission or IO error. fmt.Println(gfile.GetBytes(tempFile)) // Output: @@ -56,7 +58,8 @@ func ExamplePutContents() { tempFile = gfile.Join(tempDir, fileName) ) - // write contents + // It creates and puts content string into specifies file path. + // It automatically creates directory recursively if it does not exist. gfile.PutContents(tempFile, "goframe example content") // read contents diff --git a/os/gfile/gfile_z_example_replace_test.go b/os/gfile/gfile_z_example_replace_test.go index 9ca547aec..d9c6935b7 100644 --- a/os/gfile/gfile_z_example_replace_test.go +++ b/os/gfile/gfile_z_example_replace_test.go @@ -50,7 +50,7 @@ func ExampleReplaceFileFunc() { // read contents fmt.Println(gfile.GetContents(tempFile)) - // replace by yourself + // It replaces content directly by file path and callback function. gfile.ReplaceFileFunc(func(path, content string) string { // Replace with regular match reg, _ := regexp.Compile(`\d{3}`) @@ -78,6 +78,7 @@ func ExampleReplaceDir() { // read contents fmt.Println(gfile.GetContents(tempFile)) + // It replaces content of all files under specified directory recursively. gfile.ReplaceDir("content", "replace word", tempDir, "gflie_example.txt", true) // read contents @@ -102,7 +103,7 @@ func ExampleReplaceDirFunc() { // read contents fmt.Println(gfile.GetContents(tempFile)) - // replace by yourself + // It replaces content of all files under specified directory with custom callback function recursively. gfile.ReplaceDirFunc(func(path, content string) string { // Replace with regular match reg, _ := regexp.Compile(`\d{3}`) diff --git a/os/gfile/gfile_z_example_scan_test.go b/os/gfile/gfile_z_example_scan_test.go index b5e91eb72..0238dbbb0 100644 --- a/os/gfile/gfile_z_example_scan_test.go +++ b/os/gfile/gfile_z_example_scan_test.go @@ -55,7 +55,7 @@ func ExampleScanDirFile() { gfile.PutContents(tempSubFile, "goframe example content") // scans directory recursively exclusive of directories - list, _ := gfile.ScanDirFile(tempDir, "*", true) + list, _ := gfile.ScanDirFile(tempDir, "*.txt", true) for _, v := range list { fmt.Println(gfile.Basename(v)) } @@ -116,7 +116,7 @@ func ExampleScanDirFileFunc() { gfile.PutContents(tempSubFile, "goframe example content") // scans directory recursively exclusive of directories - list, _ := gfile.ScanDirFileFunc(tempDir, "*", true, func(path string) string { + list, _ := gfile.ScanDirFileFunc(tempDir, "*.txt", true, func(path string) string { // ignores some files if gfile.Basename(path) == "gflie_example_ignores.txt" { return "" From 0355ec6b216a9fff63031fbb6fafff110f05c096 Mon Sep 17 00:00:00 2001 From: hailaz <739476267@qq.com> Date: Fri, 19 Nov 2021 09:53:32 +0800 Subject: [PATCH 10/10] doc: add code comments --- os/gfile/gfile_z_example_contents_test.go | 3 ++- os/gfile/gfile_z_example_replace_test.go | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/os/gfile/gfile_z_example_contents_test.go b/os/gfile/gfile_z_example_contents_test.go index 7f13c6ae5..13e90500c 100644 --- a/os/gfile/gfile_z_example_contents_test.go +++ b/os/gfile/gfile_z_example_contents_test.go @@ -101,7 +101,8 @@ func ExamplePutContentsAppend() { // read contents fmt.Println(gfile.GetContents(tempFile)) - // write contents + // It creates and append content string into specifies file path. + // It automatically creates directory recursively if it does not exist. gfile.PutContentsAppend(tempFile, " append content") // read contents diff --git a/os/gfile/gfile_z_example_replace_test.go b/os/gfile/gfile_z_example_replace_test.go index d9c6935b7..df4d3a931 100644 --- a/os/gfile/gfile_z_example_replace_test.go +++ b/os/gfile/gfile_z_example_replace_test.go @@ -27,6 +27,7 @@ func ExampleReplaceFile() { // read contents fmt.Println(gfile.GetContents(tempFile)) + // It replaces content directly by file path. gfile.ReplaceFile("content", "replace word", tempFile) fmt.Println(gfile.GetContents(tempFile))