Categories: GoFrame 教程

GoFrame gfile-内容替换

内容替换

ReplaceFile

  • 说明:替换指定文件的指定内容为新内容
  • 格式:
func ReplaceFile(search, replace, path string) error 
  • 示例:
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
}

ReplaceFileFunc

  • 说明:使用自定义函数替换指定文件内容
  • 格式:
func ReplaceFileFunc(f func(path, content string) string, path string) error
  • 示例:
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]
}

ReplaceDir

  • 说明:扫描指定目录,替换符合条件的文件的指定内容为新内容
  • 格式:
func ReplaceDir(search, replace, path, pattern string, recursive ...bool) error
  • 示例:
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
}

ReplaceDirFunc

  • 说明:扫描指定目录,使用自定义函数替换符合条件的文件的指定内容为新内容
  • 格式:
func ReplaceDirFunc(f func(path, content string) string, path, pattern string, recursive ...bool) error
  • 示例:
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]

}

terry

这个人很懒,什么都没有留下~

Share
Published by
terry

Recent Posts

了解 ChatGPT 如何维护上下文

对话中的语境概念至关重要,因为…

4 天 ago

GenAI:如何通过快速压缩技术降低成本

在本文中,我们将探讨在开发早期…

1 周 ago

vue:页面注入js修改input值

一般会直接这样写: let z…

2 周 ago

聊聊vue3中的defineProps

在Vue 3中,defineP…

3 周 ago

在 Chrome 中删除、允许和管理 Cookie

您可以选择删除现有 Cooki…

4 周 ago

自定义指令:聊聊vue中的自定义指令应用法则

今天我们来聊聊vue中的自定义…

1 月 ago