github.com/wangyougui/gf/v2@v2.6.5/os/gfile/gfile_z_example_scan_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  
    12  	"github.com/wangyougui/gf/v2/os/gfile"
    13  )
    14  
    15  func ExampleScanDir() {
    16  	// init
    17  	var (
    18  		fileName = "gfile_example.txt"
    19  		tempDir  = gfile.Temp("gfile_example_scan_dir")
    20  		tempFile = gfile.Join(tempDir, fileName)
    21  
    22  		tempSubDir  = gfile.Join(tempDir, "sub_dir")
    23  		tempSubFile = gfile.Join(tempSubDir, fileName)
    24  	)
    25  
    26  	// write contents
    27  	gfile.PutContents(tempFile, "goframe example content")
    28  	gfile.PutContents(tempSubFile, "goframe example content")
    29  
    30  	// scans directory recursively
    31  	list, _ := gfile.ScanDir(tempDir, "*", true)
    32  	for _, v := range list {
    33  		fmt.Println(gfile.Basename(v))
    34  	}
    35  
    36  	// Output:
    37  	// gfile_example.txt
    38  	// sub_dir
    39  	// gfile_example.txt
    40  }
    41  
    42  func ExampleScanDirFile() {
    43  	// init
    44  	var (
    45  		fileName = "gfile_example.txt"
    46  		tempDir  = gfile.Temp("gfile_example_scan_dir_file")
    47  		tempFile = gfile.Join(tempDir, fileName)
    48  
    49  		tempSubDir  = gfile.Join(tempDir, "sub_dir")
    50  		tempSubFile = gfile.Join(tempSubDir, fileName)
    51  	)
    52  
    53  	// write contents
    54  	gfile.PutContents(tempFile, "goframe example content")
    55  	gfile.PutContents(tempSubFile, "goframe example content")
    56  
    57  	// scans directory recursively exclusive of directories
    58  	list, _ := gfile.ScanDirFile(tempDir, "*.txt", true)
    59  	for _, v := range list {
    60  		fmt.Println(gfile.Basename(v))
    61  	}
    62  
    63  	// Output:
    64  	// gfile_example.txt
    65  	// gfile_example.txt
    66  }
    67  
    68  func ExampleScanDirFunc() {
    69  	// init
    70  	var (
    71  		fileName = "gfile_example.txt"
    72  		tempDir  = gfile.Temp("gfile_example_scan_dir_func")
    73  		tempFile = gfile.Join(tempDir, fileName)
    74  
    75  		tempSubDir  = gfile.Join(tempDir, "sub_dir")
    76  		tempSubFile = gfile.Join(tempSubDir, fileName)
    77  	)
    78  
    79  	// write contents
    80  	gfile.PutContents(tempFile, "goframe example content")
    81  	gfile.PutContents(tempSubFile, "goframe example content")
    82  
    83  	// scans directory recursively
    84  	list, _ := gfile.ScanDirFunc(tempDir, "*", true, func(path string) string {
    85  		// ignores some files
    86  		if gfile.Basename(path) == "gfile_example.txt" {
    87  			return ""
    88  		}
    89  		return path
    90  	})
    91  	for _, v := range list {
    92  		fmt.Println(gfile.Basename(v))
    93  	}
    94  
    95  	// Output:
    96  	// sub_dir
    97  }
    98  
    99  func ExampleScanDirFileFunc() {
   100  	// init
   101  	var (
   102  		fileName = "gfile_example.txt"
   103  		tempDir  = gfile.Temp("gfile_example_scan_dir_file_func")
   104  		tempFile = gfile.Join(tempDir, fileName)
   105  
   106  		fileName1 = "gfile_example_ignores.txt"
   107  		tempFile1 = gfile.Join(tempDir, fileName1)
   108  
   109  		tempSubDir  = gfile.Join(tempDir, "sub_dir")
   110  		tempSubFile = gfile.Join(tempSubDir, fileName)
   111  	)
   112  
   113  	// write contents
   114  	gfile.PutContents(tempFile, "goframe example content")
   115  	gfile.PutContents(tempFile1, "goframe example content")
   116  	gfile.PutContents(tempSubFile, "goframe example content")
   117  
   118  	// scans directory recursively exclusive of directories
   119  	list, _ := gfile.ScanDirFileFunc(tempDir, "*.txt", true, func(path string) string {
   120  		// ignores some files
   121  		if gfile.Basename(path) == "gfile_example_ignores.txt" {
   122  			return ""
   123  		}
   124  		return path
   125  	})
   126  	for _, v := range list {
   127  		fmt.Println(gfile.Basename(v))
   128  	}
   129  
   130  	// Output:
   131  	// gfile_example.txt
   132  	// gfile_example.txt
   133  }