github.com/wangyougui/gf/v2@v2.6.5/os/gfile/gfile_z_exmaple_basic_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 "os" 12 13 "github.com/wangyougui/gf/v2/os/gfile" 14 ) 15 16 func ExampleMkdir() { 17 // init 18 var ( 19 path = gfile.Temp("gfile_example_basic_dir") 20 ) 21 22 // Creates directory 23 gfile.Mkdir(path) 24 25 // Check if directory exists 26 fmt.Println(gfile.IsDir(path)) 27 28 // Output: 29 // true 30 } 31 32 func ExampleCreate() { 33 // init 34 var ( 35 path = gfile.Join(gfile.Temp("gfile_example_basic_dir"), "file1") 36 dataByte = make([]byte, 50) 37 ) 38 // Check whether the file exists 39 isFile := gfile.IsFile(path) 40 41 fmt.Println(isFile) 42 43 // Creates file with given `path` recursively 44 fileHandle, _ := gfile.Create(path) 45 defer fileHandle.Close() 46 47 // Write some content to file 48 n, _ := fileHandle.WriteString("hello goframe") 49 50 // Check whether the file exists 51 isFile = gfile.IsFile(path) 52 53 fmt.Println(isFile) 54 55 // Reads len(b) bytes from the File 56 fileHandle.ReadAt(dataByte, 0) 57 58 fmt.Println(string(dataByte[:n])) 59 60 // Output: 61 // false 62 // true 63 // hello goframe 64 } 65 66 func ExampleOpen() { 67 // init 68 var ( 69 path = gfile.Join(gfile.Temp("gfile_example_basic_dir"), "file1") 70 dataByte = make([]byte, 4096) 71 ) 72 // Open file or directory with READONLY model 73 file, _ := gfile.Open(path) 74 defer file.Close() 75 76 // Read data 77 n, _ := file.Read(dataByte) 78 79 fmt.Println(string(dataByte[:n])) 80 81 // Output: 82 // hello goframe 83 } 84 85 func ExampleOpenFile() { 86 // init 87 var ( 88 path = gfile.Join(gfile.Temp("gfile_example_basic_dir"), "file1") 89 dataByte = make([]byte, 4096) 90 ) 91 // Opens file/directory with custom `flag` and `perm` 92 // Create if file does not exist,it is created in a readable and writable mode,prem 0777 93 openFile, _ := gfile.OpenFile(path, os.O_CREATE|os.O_RDWR, gfile.DefaultPermCopy) 94 defer openFile.Close() 95 96 // Write some content to file 97 writeLength, _ := openFile.WriteString("hello goframe test open file") 98 99 fmt.Println(writeLength) 100 101 // Read data 102 n, _ := openFile.ReadAt(dataByte, 0) 103 104 fmt.Println(string(dataByte[:n])) 105 106 // Output: 107 // 28 108 // hello goframe test open file 109 } 110 111 func ExampleOpenWithFlag() { 112 // init 113 var ( 114 path = gfile.Join(gfile.Temp("gfile_example_basic_dir"), "file1") 115 dataByte = make([]byte, 4096) 116 ) 117 118 // Opens file/directory with custom `flag` 119 // Create if file does not exist,it is created in a readable and writable mode with default `perm` is 0666 120 openFile, _ := gfile.OpenWithFlag(path, os.O_CREATE|os.O_RDWR) 121 defer openFile.Close() 122 123 // Write some content to file 124 writeLength, _ := openFile.WriteString("hello goframe test open file with flag") 125 126 fmt.Println(writeLength) 127 128 // Read data 129 n, _ := openFile.ReadAt(dataByte, 0) 130 131 fmt.Println(string(dataByte[:n])) 132 133 // Output: 134 // 38 135 // hello goframe test open file with flag 136 } 137 138 func ExampleJoin() { 139 // init 140 var ( 141 dirPath = gfile.Temp("gfile_example_basic_dir") 142 filePath = "file1" 143 ) 144 145 // Joins string array paths with file separator of current system. 146 joinString := gfile.Join(dirPath, filePath) 147 148 fmt.Println(joinString) 149 150 // May Output: 151 // /tmp/gfile_example_basic_dir/file1 152 } 153 154 func ExampleExists() { 155 // init 156 var ( 157 path = gfile.Join(gfile.Temp("gfile_example_basic_dir"), "file1") 158 ) 159 // Checks whether given `path` exist. 160 joinString := gfile.Exists(path) 161 162 fmt.Println(joinString) 163 164 // Output: 165 // true 166 } 167 168 func ExampleIsDir() { 169 // init 170 var ( 171 path = gfile.Temp("gfile_example_basic_dir") 172 filePath = gfile.Join(gfile.Temp("gfile_example_basic_dir"), "file1") 173 ) 174 // Checks whether given `path` a directory. 175 fmt.Println(gfile.IsDir(path)) 176 fmt.Println(gfile.IsDir(filePath)) 177 178 // Output: 179 // true 180 // false 181 } 182 183 func ExamplePwd() { 184 // Get absolute path of current working directory. 185 fmt.Println(gfile.Pwd()) 186 187 // May Output: 188 // xxx/gf/os/gfile 189 } 190 191 func ExampleChdir() { 192 // init 193 var ( 194 path = gfile.Join(gfile.Temp("gfile_example_basic_dir"), "file1") 195 ) 196 // Get current working directory 197 fmt.Println(gfile.Pwd()) 198 199 // Changes the current working directory to the named directory. 200 gfile.Chdir(path) 201 202 // Get current working directory 203 fmt.Println(gfile.Pwd()) 204 205 // May Output: 206 // xxx/gf/os/gfile 207 // /tmp/gfile_example_basic_dir/file1 208 } 209 210 func ExampleIsFile() { 211 // init 212 var ( 213 filePath = gfile.Join(gfile.Temp("gfile_example_basic_dir"), "file1") 214 dirPath = gfile.Temp("gfile_example_basic_dir") 215 ) 216 // Checks whether given `path` a file, which means it's not a directory. 217 fmt.Println(gfile.IsFile(filePath)) 218 fmt.Println(gfile.IsFile(dirPath)) 219 220 // Output: 221 // true 222 // false 223 } 224 225 func ExampleStat() { 226 // init 227 var ( 228 path = gfile.Join(gfile.Temp("gfile_example_basic_dir"), "file1") 229 ) 230 // Get a FileInfo describing the named file. 231 stat, _ := gfile.Stat(path) 232 233 fmt.Println(stat.Name()) 234 fmt.Println(stat.IsDir()) 235 fmt.Println(stat.Mode()) 236 fmt.Println(stat.ModTime()) 237 fmt.Println(stat.Size()) 238 fmt.Println(stat.Sys()) 239 240 // May Output: 241 // file1 242 // false 243 // -rwxr-xr-x 244 // 2021-12-02 11:01:27.261441694 +0800 CST 245 // &{16777220 33261 1 8597857090 501 20 0 [0 0 0 0] {1638414088 192363490} {1638414087 261441694} {1638414087 261441694} {1638413480 485068275} 38 8 4096 0 0 0 [0 0]} 246 } 247 248 func ExampleMove() { 249 // init 250 var ( 251 srcPath = gfile.Join(gfile.Temp("gfile_example_basic_dir"), "file1") 252 dstPath = gfile.Join(gfile.Temp("gfile_example_basic_dir"), "file2") 253 ) 254 // Check is file 255 fmt.Println(gfile.IsFile(dstPath)) 256 257 // Moves `src` to `dst` path. 258 // If `dst` already exists and is not a directory, it'll be replaced. 259 gfile.Move(srcPath, dstPath) 260 261 fmt.Println(gfile.IsFile(srcPath)) 262 fmt.Println(gfile.IsFile(dstPath)) 263 264 // Output: 265 // false 266 // false 267 // true 268 } 269 270 func ExampleRename() { 271 // init 272 var ( 273 srcPath = gfile.Join(gfile.Temp("gfile_example_basic_dir"), "file2") 274 dstPath = gfile.Join(gfile.Temp("gfile_example_basic_dir"), "file1") 275 ) 276 // Check is file 277 fmt.Println(gfile.IsFile(dstPath)) 278 279 // renames (moves) `src` to `dst` path. 280 // If `dst` already exists and is not a directory, it'll be replaced. 281 gfile.Rename(srcPath, dstPath) 282 283 fmt.Println(gfile.IsFile(srcPath)) 284 fmt.Println(gfile.IsFile(dstPath)) 285 286 // Output: 287 // false 288 // false 289 // true 290 } 291 292 func ExampleDirNames() { 293 // init 294 var ( 295 path = gfile.Temp("gfile_example_basic_dir") 296 ) 297 // Get sub-file names of given directory `path`. 298 dirNames, _ := gfile.DirNames(path) 299 300 fmt.Println(dirNames) 301 302 // May Output: 303 // [file1] 304 } 305 306 func ExampleGlob() { 307 // init 308 var ( 309 path = gfile.Pwd() + gfile.Separator + "*_example_basic_test.go" 310 ) 311 // Get sub-file names of given directory `path`. 312 // Only show file name 313 matchNames, _ := gfile.Glob(path, true) 314 315 fmt.Println(matchNames) 316 317 // Show full path of the file 318 matchNames, _ = gfile.Glob(path, false) 319 320 fmt.Println(matchNames) 321 322 // May Output: 323 // [gfile_z_example_basic_test.go] 324 // [xxx/gf/os/gfile/gfile_z_example_basic_test.go] 325 } 326 327 func ExampleIsReadable() { 328 // init 329 var ( 330 path = gfile.Pwd() + gfile.Separator + "testdata/readline/file.log" 331 ) 332 333 // Checks whether given `path` is readable. 334 fmt.Println(gfile.IsReadable(path)) 335 336 // Output: 337 // true 338 } 339 340 func ExampleIsWritable() { 341 // init 342 var ( 343 path = gfile.Pwd() + gfile.Separator + "testdata/readline/" 344 file = "file.log" 345 ) 346 347 // Checks whether given `path` is writable. 348 fmt.Println(gfile.IsWritable(path)) 349 fmt.Println(gfile.IsWritable(path + file)) 350 351 // Output: 352 // true 353 // true 354 } 355 356 func ExampleChmod() { 357 // init 358 var ( 359 path = gfile.Join(gfile.Temp("gfile_example_basic_dir"), "file1") 360 ) 361 362 // Get a FileInfo describing the named file. 363 stat, err := gfile.Stat(path) 364 if err != nil { 365 fmt.Println(err.Error()) 366 } 367 // Show original mode 368 fmt.Println(stat.Mode()) 369 370 // Change file model 371 gfile.Chmod(path, gfile.DefaultPermCopy) 372 373 // Get a FileInfo describing the named file. 374 stat, _ = gfile.Stat(path) 375 // Show the modified mode 376 fmt.Println(stat.Mode()) 377 378 // Output: 379 // -rw-r--r-- 380 // -rwxr-xr-x 381 } 382 383 func ExampleAbs() { 384 // init 385 var ( 386 path = gfile.Join(gfile.Temp("gfile_example_basic_dir"), "file1") 387 ) 388 389 // Get an absolute representation of path. 390 fmt.Println(gfile.Abs(path)) 391 392 // May Output: 393 // /tmp/gfile_example_basic_dir/file1 394 } 395 396 func ExampleRealPath() { 397 // init 398 var ( 399 realPath = gfile.Join(gfile.Temp("gfile_example_basic_dir"), "file1") 400 worryPath = gfile.Join(gfile.Temp("gfile_example_basic_dir"), "worryFile") 401 ) 402 403 // fetch an absolute representation of path. 404 fmt.Println(gfile.RealPath(realPath)) 405 fmt.Println(gfile.RealPath(worryPath)) 406 407 // May Output: 408 // /tmp/gfile_example_basic_dir/file1 409 } 410 411 func ExampleSelfPath() { 412 413 // Get absolute file path of current running process 414 fmt.Println(gfile.SelfPath()) 415 416 // May Output: 417 // xxx/___github_com_gogf_gf_v2_os_gfile__ExampleSelfPath 418 } 419 420 func ExampleSelfName() { 421 422 // Get file name of current running process 423 fmt.Println(gfile.SelfName()) 424 425 // May Output: 426 // ___github_com_gogf_gf_v2_os_gfile__ExampleSelfName 427 } 428 429 func ExampleSelfDir() { 430 431 // Get absolute directory path of current running process 432 fmt.Println(gfile.SelfDir()) 433 434 // May Output: 435 // /private/var/folders/p6/gc_9mm3j229c0mjrjp01gqn80000gn/T 436 } 437 438 func ExampleBasename() { 439 // init 440 var ( 441 path = gfile.Pwd() + gfile.Separator + "testdata/readline/file.log" 442 ) 443 444 // Get the last element of path, which contains file extension. 445 fmt.Println(gfile.Basename(path)) 446 447 // Output: 448 // file.log 449 } 450 451 func ExampleName() { 452 // init 453 var ( 454 path = gfile.Pwd() + gfile.Separator + "testdata/readline/file.log" 455 ) 456 457 // Get the last element of path without file extension. 458 fmt.Println(gfile.Name(path)) 459 460 // Output: 461 // file 462 } 463 464 func ExampleDir() { 465 // init 466 var ( 467 path = gfile.Join(gfile.Temp("gfile_example_basic_dir"), "file1") 468 ) 469 470 // Get all but the last element of path, typically the path's directory. 471 fmt.Println(gfile.Dir(path)) 472 473 // May Output: 474 // /tmp/gfile_example_basic_dir 475 } 476 477 func ExampleIsEmpty() { 478 // init 479 var ( 480 path = gfile.Join(gfile.Temp("gfile_example_basic_dir"), "file1") 481 ) 482 483 // Check whether the `path` is empty 484 fmt.Println(gfile.IsEmpty(path)) 485 486 // Truncate file 487 gfile.Truncate(path, 0) 488 489 // Check whether the `path` is empty 490 fmt.Println(gfile.IsEmpty(path)) 491 492 // Output: 493 // false 494 // true 495 } 496 497 func ExampleExt() { 498 // init 499 var ( 500 path = gfile.Pwd() + gfile.Separator + "testdata/readline/file.log" 501 ) 502 503 // Get the file name extension used by path. 504 fmt.Println(gfile.Ext(path)) 505 506 // Output: 507 // .log 508 } 509 510 func ExampleExtName() { 511 // init 512 var ( 513 path = gfile.Pwd() + gfile.Separator + "testdata/readline/file.log" 514 ) 515 516 // Get the file name extension used by path but the result does not contains symbol '.'. 517 fmt.Println(gfile.ExtName(path)) 518 519 // Output: 520 // log 521 } 522 523 func ExampleTempDir() { 524 // init 525 var ( 526 fileName = "gfile_example_basic_dir" 527 ) 528 529 // fetch an absolute representation of path. 530 path := gfile.Temp(fileName) 531 532 fmt.Println(path) 533 534 // May Output: 535 // /tmp/gfile_example_basic_dir 536 } 537 538 func ExampleRemove() { 539 // init 540 var ( 541 path = gfile.Join(gfile.Temp("gfile_example_basic_dir"), "file1") 542 ) 543 544 // Checks whether given `path` a file, which means it's not a directory. 545 fmt.Println(gfile.IsFile(path)) 546 547 // deletes all file/directory with `path` parameter. 548 gfile.Remove(path) 549 550 // Check again 551 fmt.Println(gfile.IsFile(path)) 552 553 // Output: 554 // true 555 // false 556 }