mirror of
https://gitee.com/johng/gf
synced 2026-06-06 02:25:47 +08:00
test: add gflie example
This commit is contained in:
@ -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()
|
||||
|
||||
@ -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)
|
||||
|
||||
43
os/gfile/gfile_z_example_cache_test.go
Normal file
43
os/gfile/gfile_z_example_cache_test.go
Normal file
@ -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
|
||||
}
|
||||
253
os/gfile/gfile_z_example_contents_test.go
Normal file
253
os/gfile/gfile_z_example_contents_test.go
Normal file
@ -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
|
||||
}
|
||||
53
os/gfile/gfile_z_example_copy_test.go
Normal file
53
os/gfile/gfile_z_example_copy_test.go
Normal file
@ -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
|
||||
}
|
||||
16
os/gfile/gfile_z_example_home_test.go
Normal file
16
os/gfile/gfile_z_example_home_test.go
Normal file
@ -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
|
||||
}
|
||||
145
os/gfile/gfile_z_example_replace_test.go
Normal file
145
os/gfile/gfile_z_example_replace_test.go
Normal file
@ -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
|
||||
}
|
||||
117
os/gfile/gfile_z_example_scan_test.go
Normal file
117
os/gfile/gfile_z_example_scan_test.go
Normal file
@ -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
|
||||
}
|
||||
23
os/gfile/gfile_z_example_search_test.go
Normal file
23
os/gfile/gfile_z_example_search_test.go
Normal file
@ -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
|
||||
}
|
||||
56
os/gfile/gfile_z_example_size_test.go
Normal file
56
os/gfile/gfile_z_example_size_test.go
Normal file
@ -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
|
||||
}
|
||||
26
os/gfile/gfile_z_example_sort_test.go
Normal file
26
os/gfile/gfile_z_example_sort_test.go
Normal file
@ -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]
|
||||
}
|
||||
31
os/gfile/gfile_z_example_time_test.go
Normal file
31
os/gfile/gfile_z_example_time_test.go
Normal file
@ -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
|
||||
}
|
||||
@ -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)
|
||||
|
||||
Reference in New Issue
Block a user