Files
gf/os/gfile/gfile_z_example_replace_test.go

121 lines
2.8 KiB
Go
Raw Permalink Normal View History

2021-11-17 14:14:19 +08:00
// 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.
2021-11-05 14:30:50 +08:00
package gfile_test
import (
"fmt"
"regexp"
"github.com/gogf/gf/v2/os/gfile"
)
func ExampleReplaceFile() {
// init
2021-11-11 13:01:32 +08:00
var (
2024-01-02 20:17:54 +08:00
fileName = "gfile_example.txt"
tempDir = gfile.Temp("gfile_example_replace")
2021-11-11 13:01:32 +08:00
tempFile = gfile.Join(tempDir, fileName)
)
2021-11-05 14:30:50 +08:00
// write contents
2021-11-11 13:01:32 +08:00
gfile.PutContents(tempFile, "goframe example content")
2021-11-05 14:30:50 +08:00
// read contents
2021-11-11 13:01:32 +08:00
fmt.Println(gfile.GetContents(tempFile))
2021-11-05 14:30:50 +08:00
2021-11-19 09:53:32 +08:00
// It replaces content directly by file path.
2021-11-11 13:01:32 +08:00
gfile.ReplaceFile("content", "replace word", tempFile)
2021-11-05 14:30:50 +08:00
2021-11-11 13:01:32 +08:00
fmt.Println(gfile.GetContents(tempFile))
2021-11-05 14:30:50 +08:00
// Output:
2021-11-11 13:01:32 +08:00
// goframe example content
// goframe example replace word
2021-11-05 14:30:50 +08:00
}
func ExampleReplaceFileFunc() {
// init
2021-11-11 13:01:32 +08:00
var (
2024-01-02 20:17:54 +08:00
fileName = "gfile_example.txt"
tempDir = gfile.Temp("gfile_example_replace")
2021-11-11 13:01:32 +08:00
tempFile = gfile.Join(tempDir, fileName)
)
2021-11-05 14:30:50 +08:00
// write contents
2021-11-11 13:01:32 +08:00
gfile.PutContents(tempFile, "goframe example 123")
2021-11-05 14:30:50 +08:00
// read contents
2021-11-11 13:01:32 +08:00
fmt.Println(gfile.GetContents(tempFile))
2021-11-05 14:30:50 +08:00
2021-11-17 19:50:03 +08:00
// It replaces content directly by file path and callback function.
2021-11-05 14:30:50 +08:00
gfile.ReplaceFileFunc(func(path, content string) string {
// Replace with regular match
reg, _ := regexp.Compile(`\d{3}`)
return reg.ReplaceAllString(content, "[num]")
}, tempFile)
2021-11-11 13:01:32 +08:00
fmt.Println(gfile.GetContents(tempFile))
2021-11-05 14:30:50 +08:00
// Output:
2021-11-11 13:01:32 +08:00
// goframe example 123
// goframe example [num]
2021-11-05 14:30:50 +08:00
}
func ExampleReplaceDir() {
// init
2021-11-11 13:01:32 +08:00
var (
2024-01-02 20:17:54 +08:00
fileName = "gfile_example.txt"
tempDir = gfile.Temp("gfile_example_replace")
2021-11-11 13:01:32 +08:00
tempFile = gfile.Join(tempDir, fileName)
)
2021-11-05 14:30:50 +08:00
// write contents
2021-11-11 13:01:32 +08:00
gfile.PutContents(tempFile, "goframe example content")
2021-11-05 14:30:50 +08:00
// read contents
2021-11-11 13:01:32 +08:00
fmt.Println(gfile.GetContents(tempFile))
2021-11-05 14:30:50 +08:00
2021-11-17 19:50:03 +08:00
// It replaces content of all files under specified directory recursively.
2024-01-02 20:17:54 +08:00
gfile.ReplaceDir("content", "replace word", tempDir, "gfile_example.txt", true)
2021-11-05 14:30:50 +08:00
// read contents
2021-11-11 13:01:32 +08:00
fmt.Println(gfile.GetContents(tempFile))
2021-11-05 14:30:50 +08:00
// Output:
2021-11-11 13:01:32 +08:00
// goframe example content
// goframe example replace word
2021-11-05 14:30:50 +08:00
}
func ExampleReplaceDirFunc() {
// init
2021-11-11 13:01:32 +08:00
var (
2024-01-02 20:17:54 +08:00
fileName = "gfile_example.txt"
tempDir = gfile.Temp("gfile_example_replace")
2021-11-11 13:01:32 +08:00
tempFile = gfile.Join(tempDir, fileName)
)
2021-11-05 14:30:50 +08:00
// write contents
2021-11-11 13:01:32 +08:00
gfile.PutContents(tempFile, "goframe example 123")
2021-11-05 14:30:50 +08:00
// read contents
2021-11-11 13:01:32 +08:00
fmt.Println(gfile.GetContents(tempFile))
2021-11-05 14:30:50 +08:00
2021-11-17 19:50:03 +08:00
// It replaces content of all files under specified directory with custom callback function recursively.
2021-11-05 14:30:50 +08:00
gfile.ReplaceDirFunc(func(path, content string) string {
// Replace with regular match
reg, _ := regexp.Compile(`\d{3}`)
return reg.ReplaceAllString(content, "[num]")
2024-01-02 20:17:54 +08:00
}, tempDir, "gfile_example.txt", true)
2021-11-05 14:30:50 +08:00
2021-11-11 13:01:32 +08:00
fmt.Println(gfile.GetContents(tempFile))
2021-11-05 14:30:50 +08:00
// Output:
2021-11-11 13:01:32 +08:00
// goframe example 123
// goframe example [num]
2021-11-05 14:30:50 +08:00
}