github.com/wangyougui/gf/v2@v2.6.5/os/gfile/gfile_z_example_replace_test.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/wangyougui/gf. 6 7 package gfile_test 8 9 import ( 10 "fmt" 11 "regexp" 12 13 "github.com/wangyougui/gf/v2/os/gfile" 14 ) 15 16 func ExampleReplaceFile() { 17 // init 18 var ( 19 fileName = "gfile_example.txt" 20 tempDir = gfile.Temp("gfile_example_replace") 21 tempFile = gfile.Join(tempDir, fileName) 22 ) 23 24 // write contents 25 gfile.PutContents(tempFile, "goframe example content") 26 27 // read contents 28 fmt.Println(gfile.GetContents(tempFile)) 29 30 // It replaces content directly by file path. 31 gfile.ReplaceFile("content", "replace word", tempFile) 32 33 fmt.Println(gfile.GetContents(tempFile)) 34 35 // Output: 36 // goframe example content 37 // goframe example replace word 38 } 39 40 func ExampleReplaceFileFunc() { 41 // init 42 var ( 43 fileName = "gfile_example.txt" 44 tempDir = gfile.Temp("gfile_example_replace") 45 tempFile = gfile.Join(tempDir, fileName) 46 ) 47 48 // write contents 49 gfile.PutContents(tempFile, "goframe example 123") 50 51 // read contents 52 fmt.Println(gfile.GetContents(tempFile)) 53 54 // It replaces content directly by file path and callback function. 55 gfile.ReplaceFileFunc(func(path, content string) string { 56 // Replace with regular match 57 reg, _ := regexp.Compile(`\d{3}`) 58 return reg.ReplaceAllString(content, "[num]") 59 }, tempFile) 60 61 fmt.Println(gfile.GetContents(tempFile)) 62 63 // Output: 64 // goframe example 123 65 // goframe example [num] 66 } 67 68 func ExampleReplaceDir() { 69 // init 70 var ( 71 fileName = "gfile_example.txt" 72 tempDir = gfile.Temp("gfile_example_replace") 73 tempFile = gfile.Join(tempDir, fileName) 74 ) 75 76 // write contents 77 gfile.PutContents(tempFile, "goframe example content") 78 79 // read contents 80 fmt.Println(gfile.GetContents(tempFile)) 81 82 // It replaces content of all files under specified directory recursively. 83 gfile.ReplaceDir("content", "replace word", tempDir, "gfile_example.txt", true) 84 85 // read contents 86 fmt.Println(gfile.GetContents(tempFile)) 87 88 // Output: 89 // goframe example content 90 // goframe example replace word 91 } 92 93 func ExampleReplaceDirFunc() { 94 // init 95 var ( 96 fileName = "gfile_example.txt" 97 tempDir = gfile.Temp("gfile_example_replace") 98 tempFile = gfile.Join(tempDir, fileName) 99 ) 100 101 // write contents 102 gfile.PutContents(tempFile, "goframe example 123") 103 104 // read contents 105 fmt.Println(gfile.GetContents(tempFile)) 106 107 // It replaces content of all files under specified directory with custom callback function recursively. 108 gfile.ReplaceDirFunc(func(path, content string) string { 109 // Replace with regular match 110 reg, _ := regexp.Compile(`\d{3}`) 111 return reg.ReplaceAllString(content, "[num]") 112 }, tempDir, "gfile_example.txt", true) 113 114 fmt.Println(gfile.GetContents(tempFile)) 115 116 // Output: 117 // goframe example 123 118 // goframe example [num] 119 120 }