mirror of
https://gitee.com/johng/gf
synced 2026-06-06 02:25:47 +08:00
doc: add code comments
This commit is contained in:
@ -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
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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}`)
|
||||
|
||||
@ -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 ""
|
||||
|
||||
Reference in New Issue
Block a user