diff --git a/os/gfile/gfile_home.go b/os/gfile/gfile_home.go index f75de43fe..68bc6c645 100644 --- a/os/gfile/gfile_home.go +++ b/os/gfile/gfile_home.go @@ -38,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_example_cache_test.go b/os/gfile/gfile_z_example_cache_test.go new file mode 100644 index 000000000..3ca819779 --- /dev/null +++ b/os/gfile/gfile_z_example_cache_test.go @@ -0,0 +1,43 @@ +// 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 ( + "fmt" + "time" + + "github.com/gogf/gf/v2/os/gfile" +) + +func ExampleGetContentsWithCache() { + // init + var ( + fileName = "gflie_example.txt" + tempDir = gfile.TempDir("gfile_example_cache") + tempFile = gfile.Join(tempDir, fileName) + ) + + // write contents + gfile.PutContents(tempFile, "goframe example content") + + // 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 + fmt.Println(gfile.GetContentsWithCache(tempFile)) + + // May Output: + // 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 new file mode 100644 index 000000000..13e90500c --- /dev/null +++ b/os/gfile/gfile_z_example_contents_test.go @@ -0,0 +1,240 @@ +// 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 ( + "fmt" + + "github.com/gogf/gf/v2/os/gfile" +) + +func ExampleGetContents() { + // init + var ( + fileName = "gflie_example.txt" + tempDir = gfile.TempDir("gfile_example_content") + tempFile = gfile.Join(tempDir, fileName) + ) + + // write contents + gfile.PutContents(tempFile, "goframe example content") + + // 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: + // goframe example content +} + +func ExampleGetBytes() { + // init + var ( + fileName = "gflie_example.txt" + tempDir = gfile.TempDir("gfile_example_content") + tempFile = gfile.Join(tempDir, fileName) + ) + + // write contents + gfile.PutContents(tempFile, "goframe example content") + + // 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: + // [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 + var ( + fileName = "gflie_example.txt" + tempDir = gfile.TempDir("gfile_example_content") + tempFile = gfile.Join(tempDir, fileName) + ) + + // 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 + fmt.Println(gfile.GetContents(tempFile)) + + // Output: + // goframe example content +} + +func ExamplePutBytes() { + // init + var ( + fileName = "gflie_example.txt" + tempDir = gfile.TempDir("gfile_example_content") + tempFile = gfile.Join(tempDir, fileName) + ) + + // write contents + gfile.PutBytes(tempFile, []byte("goframe example content")) + + // read contents + fmt.Println(gfile.GetContents(tempFile)) + + // Output: + // goframe example content +} + +func ExamplePutContentsAppend() { + // init + var ( + fileName = "gflie_example.txt" + tempDir = gfile.TempDir("gfile_example_content") + tempFile = gfile.Join(tempDir, fileName) + ) + + // write contents + gfile.PutContents(tempFile, "goframe example content") + + // read contents + fmt.Println(gfile.GetContents(tempFile)) + + // 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 + fmt.Println(gfile.GetContents(tempFile)) + + // Output: + // goframe example content + // goframe example content append content +} + +func ExamplePutBytesAppend() { + // init + var ( + fileName = "gflie_example.txt" + tempDir = gfile.TempDir("gfile_example_content") + tempFile = gfile.Join(tempDir, fileName) + ) + + // write contents + gfile.PutContents(tempFile, "goframe example content") + + // read contents + fmt.Println(gfile.GetContents(tempFile)) + + // write contents + gfile.PutBytesAppend(tempFile, []byte(" append")) + + // read contents + fmt.Println(gfile.GetContents(tempFile)) + + // Output: + // goframe example content + // goframe example content append +} + +func ExampleGetNextCharOffsetByPath() { + // init + var ( + fileName = "gflie_example.txt" + tempDir = gfile.TempDir("gfile_example_content") + tempFile = gfile.Join(tempDir, fileName) + ) + + // write contents + gfile.PutContents(tempFile, "goframe example content") + + // read contents + index := gfile.GetNextCharOffsetByPath(tempFile, 'f', 0) + fmt.Println(index) + + // Output: + // 2 +} + +func ExampleGetBytesTilCharByPath() { + // init + var ( + fileName = "gflie_example.txt" + tempDir = gfile.TempDir("gfile_example_content") + tempFile = gfile.Join(tempDir, fileName) + ) + + // write contents + gfile.PutContents(tempFile, "goframe example content") + + // read contents + fmt.Println(gfile.GetBytesTilCharByPath(tempFile, 'f', 0)) + + // Output: + // [103 111 102] 2 +} + +func ExampleGetBytesByTwoOffsetsByPath() { + // init + var ( + fileName = "gflie_example.txt" + tempDir = gfile.TempDir("gfile_example_content") + tempFile = gfile.Join(tempDir, fileName) + ) + + // write contents + gfile.PutContents(tempFile, "goframe example content") + + // read contents + fmt.Println(gfile.GetBytesByTwoOffsetsByPath(tempFile, 0, 7)) + + // Output: + // [103 111 102 114 97 109 101] +} + +func ExampleReadLines() { + // init + var ( + fileName = "gflie_example.txt" + tempDir = gfile.TempDir("gfile_example_content") + tempFile = gfile.Join(tempDir, fileName) + ) + + // write contents + gfile.PutContents(tempFile, "L1 goframe example content\nL2 goframe example content") + + // read contents + gfile.ReadLines(tempFile, func(text string) error { + // Process each line + fmt.Println(text) + return nil + }) + + // Output: + // L1 goframe example content + // L2 goframe example content +} + +func ExampleReadLinesBytes() { + // init + var ( + fileName = "gflie_example.txt" + tempDir = gfile.TempDir("gfile_example_content") + tempFile = gfile.Join(tempDir, fileName) + ) + + // write 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(bytes) + return nil + }) + + // Output: + // [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 new file mode 100644 index 000000000..293d40a82 --- /dev/null +++ b/os/gfile/gfile_z_example_copy_test.go @@ -0,0 +1,52 @@ +// 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 ( + "fmt" + + "github.com/gogf/gf/v2/os/gfile" +) + +func ExampleCopy() { + // init + var ( + srcFileName = "gflie_example.txt" + srcTempDir = gfile.TempDir("gfile_example_copy_src") + srcTempFile = gfile.Join(srcTempDir, srcFileName) + + // copy file + dstFileName = "gflie_example_copy.txt" + dstTempFile = gfile.Join(srcTempDir, dstFileName) + + // copy dir + dstTempDir = gfile.TempDir("gfile_example_copy_dst") + ) + + // write contents + gfile.PutContents(srcTempFile, "goframe example copy") + + // copy file + gfile.Copy(srcTempFile, dstTempFile) + + // read contents after copy file + fmt.Println(gfile.GetContents(dstTempFile)) + + // copy dir + gfile.Copy(srcTempDir, dstTempDir) + + // list copy dir file + fList, _ := gfile.ScanDir(dstTempDir, "*", false) + for _, v := range fList { + fmt.Println(gfile.Basename(v)) + } + + // Output: + // goframe example copy + // gflie_example.txt + // gflie_example_copy.txt +} 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..9cc2b9707 --- /dev/null +++ b/os/gfile/gfile_z_example_home_test.go @@ -0,0 +1,22 @@ +// 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 ( + "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..df4d3a931 --- /dev/null +++ b/os/gfile/gfile_z_example_replace_test.go @@ -0,0 +1,120 @@ +// 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 ( + "fmt" + "regexp" + + "github.com/gogf/gf/v2/os/gfile" +) + +func ExampleReplaceFile() { + // init + var ( + fileName = "gflie_example.txt" + tempDir = gfile.TempDir("gfile_example_replace") + tempFile = gfile.Join(tempDir, fileName) + ) + + // write contents + gfile.PutContents(tempFile, "goframe example content") + + // 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)) + + // Output: + // goframe example content + // goframe example replace word +} + +func ExampleReplaceFileFunc() { + // init + var ( + fileName = "gflie_example.txt" + tempDir = gfile.TempDir("gfile_example_replace") + tempFile = gfile.Join(tempDir, fileName) + ) + + // write contents + gfile.PutContents(tempFile, "goframe example 123") + + // read contents + fmt.Println(gfile.GetContents(tempFile)) + + // 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}`) + return reg.ReplaceAllString(content, "[num]") + }, tempFile) + + fmt.Println(gfile.GetContents(tempFile)) + + // Output: + // goframe example 123 + // goframe example [num] +} + +func ExampleReplaceDir() { + // init + var ( + fileName = "gflie_example.txt" + tempDir = gfile.TempDir("gfile_example_replace") + tempFile = gfile.Join(tempDir, fileName) + ) + + // write contents + gfile.PutContents(tempFile, "goframe example content") + + // 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 + fmt.Println(gfile.GetContents(tempFile)) + + // Output: + // goframe example content + // goframe example replace word +} + +func ExampleReplaceDirFunc() { + // init + var ( + fileName = "gflie_example.txt" + tempDir = gfile.TempDir("gfile_example_replace") + tempFile = gfile.Join(tempDir, fileName) + ) + + // write contents + gfile.PutContents(tempFile, "goframe example 123") + + // read contents + fmt.Println(gfile.GetContents(tempFile)) + + // 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}`) + return reg.ReplaceAllString(content, "[num]") + }, tempDir, "gflie_example.txt", true) + + fmt.Println(gfile.GetContents(tempFile)) + + // Output: + // 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 new file mode 100644 index 000000000..0238dbbb0 --- /dev/null +++ b/os/gfile/gfile_z_example_scan_test.go @@ -0,0 +1,133 @@ +// 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 ( + "fmt" + + "github.com/gogf/gf/v2/os/gfile" +) + +func ExampleScanDir() { + // init + var ( + fileName = "gflie_example.txt" + tempDir = gfile.TempDir("gfile_example_scan_dir") + tempFile = gfile.Join(tempDir, fileName) + + tempSubDir = gfile.Join(tempDir, "sub_dir") + tempSubFile = gfile.Join(tempSubDir, fileName) + ) + + // write contents + gfile.PutContents(tempFile, "goframe example content") + gfile.PutContents(tempSubFile, "goframe example content") + + // scans directory recursively + list, _ := gfile.ScanDir(tempDir, "*", true) + for _, v := range list { + fmt.Println(gfile.Basename(v)) + } + + // Output: + // gflie_example.txt + // sub_dir + // gflie_example.txt +} + +func ExampleScanDirFile() { + // init + var ( + fileName = "gflie_example.txt" + tempDir = gfile.TempDir("gfile_example_scan_dir_file") + tempFile = gfile.Join(tempDir, fileName) + + tempSubDir = gfile.Join(tempDir, "sub_dir") + tempSubFile = gfile.Join(tempSubDir, fileName) + ) + + // write contents + gfile.PutContents(tempFile, "goframe example content") + gfile.PutContents(tempSubFile, "goframe example content") + + // scans directory recursively exclusive of directories + list, _ := gfile.ScanDirFile(tempDir, "*.txt", true) + for _, v := range list { + fmt.Println(gfile.Basename(v)) + } + + // Output: + // gflie_example.txt + // gflie_example.txt +} + +func ExampleScanDirFunc() { + // init + var ( + fileName = "gflie_example.txt" + tempDir = gfile.TempDir("gfile_example_scan_dir_func") + tempFile = gfile.Join(tempDir, fileName) + + tempSubDir = gfile.Join(tempDir, "sub_dir") + tempSubFile = gfile.Join(tempSubDir, fileName) + ) + + // write contents + gfile.PutContents(tempFile, "goframe example content") + gfile.PutContents(tempSubFile, "goframe example content") + + // scans directory recursively + list, _ := gfile.ScanDirFunc(tempDir, "*", true, func(path string) string { + // ignores some files + if gfile.Basename(path) == "gflie_example.txt" { + return "" + } + return path + }) + for _, v := range list { + fmt.Println(gfile.Basename(v)) + } + + // Output: + // sub_dir +} + +func ExampleScanDirFileFunc() { + // init + var ( + fileName = "gflie_example.txt" + tempDir = gfile.TempDir("gfile_example_scan_dir_file_func") + tempFile = gfile.Join(tempDir, fileName) + + fileName1 = "gflie_example_ignores.txt" + tempFile1 = gfile.Join(tempDir, fileName1) + + 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, "*.txt", true, func(path string) string { + // ignores some files + if gfile.Basename(path) == "gflie_example_ignores.txt" { + return "" + } + return path + }) + for _, v := range list { + fmt.Println(gfile.Basename(v)) + } + + // Output: + // 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 new file mode 100644 index 000000000..3fdaf4309 --- /dev/null +++ b/os/gfile/gfile_z_example_search_test.go @@ -0,0 +1,32 @@ +// 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 ( + "fmt" + + "github.com/gogf/gf/v2/os/gfile" +) + +func ExampleSearch() { + // init + 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: + // gflie_example.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..b4696e6bc --- /dev/null +++ b/os/gfile/gfile_z_example_size_test.go @@ -0,0 +1,83 @@ +// 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 ( + "fmt" + + "github.com/gogf/gf/v2/os/gfile" +) + +func ExampleSize() { + // 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: + // 10 +} + +func ExampleSizeFormat() { + // 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: + // 10.00B +} + +func ExampleReadableSize() { + // 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: + // 20.00B +} + +func ExampleStrToSize() { + size := gfile.StrToSize("100MB") + fmt.Println(size) + + // Output: + // 104857600 +} + +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 +} 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..aaa46d2b6 --- /dev/null +++ b/os/gfile/gfile_z_example_sort_test.go @@ -0,0 +1,32 @@ +// 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 ( + "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..e4a11ef51 --- /dev/null +++ b/os/gfile/gfile_z_example_time_test.go @@ -0,0 +1,37 @@ +// 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 ( + "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_unit_contents_test.go b/os/gfile/gfile_z_unit_contents_test.go index bd972d523..3d38e18a6 100644 --- a/os/gfile/gfile_z_unit_contents_test.go +++ b/os/gfile/gfile_z_unit_contents_test.go @@ -103,8 +103,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_unit_test.go b/os/gfile/gfile_z_unit_test.go index 60d7bf7ba..4ef155e26 100644 --- a/os/gfile/gfile_z_unit_test.go +++ b/os/gfile/gfile_z_unit_test.go @@ -190,7 +190,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)