github.com/wangyougui/gf/v2@v2.6.5/os/gfile/gfile_z_example_copy_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 ExampleCopy() {
    16  	// init
    17  	var (
    18  		srcFileName = "gfile_example.txt"
    19  		srcTempDir  = gfile.Temp("gfile_example_copy_src")
    20  		srcTempFile = gfile.Join(srcTempDir, srcFileName)
    21  
    22  		// copy file
    23  		dstFileName = "gfile_example_copy.txt"
    24  		dstTempFile = gfile.Join(srcTempDir, dstFileName)
    25  
    26  		// copy dir
    27  		dstTempDir = gfile.Temp("gfile_example_copy_dst")
    28  	)
    29  
    30  	// write contents
    31  	gfile.PutContents(srcTempFile, "goframe example copy")
    32  
    33  	// copy file
    34  	gfile.Copy(srcTempFile, dstTempFile)
    35  
    36  	// read contents after copy file
    37  	fmt.Println(gfile.GetContents(dstTempFile))
    38  
    39  	// copy dir
    40  	gfile.Copy(srcTempDir, dstTempDir)
    41  
    42  	// list copy dir file
    43  	fList, _ := gfile.ScanDir(dstTempDir, "*", false)
    44  	for _, v := range fList {
    45  		fmt.Println(gfile.Basename(v))
    46  	}
    47  
    48  	// Output:
    49  	// goframe example copy
    50  	// gfile_example.txt
    51  	// gfile_example_copy.txt
    52  }