十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
func ReplaceFile(search, replace, path string) errorfunc 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 ReplaceFileFunc(f func(path, content string) string, path string) errorfunc 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 ReplaceDir(search, replace, path, pattern string, recursive ...bool) errorfunc 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 ReplaceDirFunc(f func(path, content string) string, path, pattern string, recursive ...bool) errorfunc 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]
}