日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)GoFrame教程:GoFrame gfile-內(nèi)容替換

內(nèi)容替換

ReplaceFile

  • 說明:替換指定文件的指定內(nèi)容為新內(nèi)容
  • 格式: 
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

  • 說明:使用自定義函數(shù)替換指定文件內(nèi)容
  • 格式: 
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

  • 說明:掃描指定目錄,替換符合條件的文件的指定內(nèi)容為新內(nèi)容
  • 格式: 
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

  • 說明:掃描指定目錄,使用自定義函數(shù)替換符合條件的文件的指定內(nèi)容為新內(nèi)容
  • 格式: 
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]

}

網(wǎng)站標題:創(chuàng)新互聯(lián)GoFrame教程:GoFrame gfile-內(nèi)容替換
網(wǎng)站鏈接:http://www.dlmjj.cn/article/dpjcgde.html