github.com/gogf/gf/v2@v2.7.4/os/gfile/gfile_z_example_contents_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/gogf/gf. 6 7 package gfile_test 8 9 import ( 10 "fmt" 11 12 "github.com/gogf/gf/v2/os/gfile" 13 ) 14 15 func ExampleGetContents() { 16 // init 17 var ( 18 fileName = "gfile_example.txt" 19 tempDir = gfile.Temp("gfile_example_content") 20 tempFile = gfile.Join(tempDir, fileName) 21 ) 22 23 // write contents 24 gfile.PutContents(tempFile, "goframe example content") 25 26 // It reads and returns the file content as string. 27 // It returns empty string if it fails reading, for example, with permission or IO error. 28 fmt.Println(gfile.GetContents(tempFile)) 29 30 // Output: 31 // goframe example content 32 } 33 34 func ExampleGetBytes() { 35 // init 36 var ( 37 fileName = "gfile_example.txt" 38 tempDir = gfile.Temp("gfile_example_content") 39 tempFile = gfile.Join(tempDir, fileName) 40 ) 41 42 // write contents 43 gfile.PutContents(tempFile, "goframe example content") 44 45 // It reads and returns the file content as []byte. 46 // It returns nil if it fails reading, for example, with permission or IO error. 47 fmt.Println(gfile.GetBytes(tempFile)) 48 49 // Output: 50 // [103 111 102 114 97 109 101 32 101 120 97 109 112 108 101 32 99 111 110 116 101 110 116] 51 } 52 53 func ExamplePutContents() { 54 // init 55 var ( 56 fileName = "gfile_example.txt" 57 tempDir = gfile.Temp("gfile_example_content") 58 tempFile = gfile.Join(tempDir, fileName) 59 ) 60 61 // It creates and puts content string into specifies file path. 62 // It automatically creates directory recursively if it does not exist. 63 gfile.PutContents(tempFile, "goframe example content") 64 65 // read contents 66 fmt.Println(gfile.GetContents(tempFile)) 67 68 // Output: 69 // goframe example content 70 } 71 72 func ExamplePutBytes() { 73 // init 74 var ( 75 fileName = "gfile_example.txt" 76 tempDir = gfile.Temp("gfile_example_content") 77 tempFile = gfile.Join(tempDir, fileName) 78 ) 79 80 // write contents 81 gfile.PutBytes(tempFile, []byte("goframe example content")) 82 83 // read contents 84 fmt.Println(gfile.GetContents(tempFile)) 85 86 // Output: 87 // goframe example content 88 } 89 90 func ExamplePutContentsAppend() { 91 // init 92 var ( 93 fileName = "gfile_example.txt" 94 tempDir = gfile.Temp("gfile_example_content") 95 tempFile = gfile.Join(tempDir, fileName) 96 ) 97 98 // write contents 99 gfile.PutContents(tempFile, "goframe example content") 100 101 // read contents 102 fmt.Println(gfile.GetContents(tempFile)) 103 104 // It creates and append content string into specifies file path. 105 // It automatically creates directory recursively if it does not exist. 106 gfile.PutContentsAppend(tempFile, " append content") 107 108 // read contents 109 fmt.Println(gfile.GetContents(tempFile)) 110 111 // Output: 112 // goframe example content 113 // goframe example content append content 114 } 115 116 func ExamplePutBytesAppend() { 117 // init 118 var ( 119 fileName = "gfile_example.txt" 120 tempDir = gfile.Temp("gfile_example_content") 121 tempFile = gfile.Join(tempDir, fileName) 122 ) 123 124 // write contents 125 gfile.PutContents(tempFile, "goframe example content") 126 127 // read contents 128 fmt.Println(gfile.GetContents(tempFile)) 129 130 // write contents 131 gfile.PutBytesAppend(tempFile, []byte(" append")) 132 133 // read contents 134 fmt.Println(gfile.GetContents(tempFile)) 135 136 // Output: 137 // goframe example content 138 // goframe example content append 139 } 140 141 func ExampleGetNextCharOffsetByPath() { 142 // init 143 var ( 144 fileName = "gfile_example.txt" 145 tempDir = gfile.Temp("gfile_example_content") 146 tempFile = gfile.Join(tempDir, fileName) 147 ) 148 149 // write contents 150 gfile.PutContents(tempFile, "goframe example content") 151 152 // read contents 153 index := gfile.GetNextCharOffsetByPath(tempFile, 'f', 0) 154 fmt.Println(index) 155 156 // Output: 157 // 2 158 } 159 160 func ExampleGetBytesTilCharByPath() { 161 // init 162 var ( 163 fileName = "gfile_example.txt" 164 tempDir = gfile.Temp("gfile_example_content") 165 tempFile = gfile.Join(tempDir, fileName) 166 ) 167 168 // write contents 169 gfile.PutContents(tempFile, "goframe example content") 170 171 // read contents 172 fmt.Println(gfile.GetBytesTilCharByPath(tempFile, 'f', 0)) 173 174 // Output: 175 // [103 111 102] 2 176 } 177 178 func ExampleGetBytesByTwoOffsetsByPath() { 179 // init 180 var ( 181 fileName = "gfile_example.txt" 182 tempDir = gfile.Temp("gfile_example_content") 183 tempFile = gfile.Join(tempDir, fileName) 184 ) 185 186 // write contents 187 gfile.PutContents(tempFile, "goframe example content") 188 189 // read contents 190 fmt.Println(gfile.GetBytesByTwoOffsetsByPath(tempFile, 0, 7)) 191 192 // Output: 193 // [103 111 102 114 97 109 101] 194 } 195 196 func ExampleReadLines() { 197 // init 198 var ( 199 fileName = "gfile_example.txt" 200 tempDir = gfile.Temp("gfile_example_content") 201 tempFile = gfile.Join(tempDir, fileName) 202 ) 203 204 // write contents 205 gfile.PutContents(tempFile, "L1 goframe example content\nL2 goframe example content") 206 207 // read contents 208 gfile.ReadLines(tempFile, func(text string) error { 209 // Process each line 210 fmt.Println(text) 211 return nil 212 }) 213 214 // Output: 215 // L1 goframe example content 216 // L2 goframe example content 217 } 218 219 func ExampleReadLinesBytes() { 220 // init 221 var ( 222 fileName = "gfile_example.txt" 223 tempDir = gfile.Temp("gfile_example_content") 224 tempFile = gfile.Join(tempDir, fileName) 225 ) 226 227 // write contents 228 gfile.PutContents(tempFile, "L1 goframe example content\nL2 goframe example content") 229 230 // read contents 231 gfile.ReadLinesBytes(tempFile, func(bytes []byte) error { 232 // Process each line 233 fmt.Println(bytes) 234 return nil 235 }) 236 237 // Output: 238 // [76 49 32 103 111 102 114 97 109 101 32 101 120 97 109 112 108 101 32 99 111 110 116 101 110 116] 239 // [76 50 32 103 111 102 114 97 109 101 32 101 120 97 109 112 108 101 32 99 111 110 116 101 110 116] 240 }