Files
gf/os/gfile/gfile_z_example_copy_test.go

53 lines
1.1 KiB
Go
Raw Permalink Normal View History

2021-11-17 14:14:19 +08:00
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
2021-11-05 14:30:50 +08:00
package gfile_test
import (
"fmt"
"github.com/gogf/gf/v2/os/gfile"
)
func ExampleCopy() {
// init
2021-11-11 13:01:32 +08:00
var (
srcFileName = "gflie_example.txt"
srcTempDir = gfile.Temp("gfile_example_copy_src")
2021-11-11 13:01:32 +08:00
srcTempFile = gfile.Join(srcTempDir, srcFileName)
2021-11-05 14:30:50 +08:00
2021-11-11 13:01:32 +08:00
// copy file
dstFileName = "gflie_example_copy.txt"
dstTempFile = gfile.Join(srcTempDir, dstFileName)
2021-11-05 14:30:50 +08:00
2021-11-11 13:01:32 +08:00
// copy dir
dstTempDir = gfile.Temp("gfile_example_copy_dst")
2021-11-11 13:01:32 +08:00
)
2021-11-05 14:30:50 +08:00
// write contents
2021-11-11 13:01:32 +08:00
gfile.PutContents(srcTempFile, "goframe example copy")
2021-11-05 14:30:50 +08:00
// copy file
2021-11-11 13:01:32 +08:00
gfile.Copy(srcTempFile, dstTempFile)
2021-11-05 14:30:50 +08:00
2021-11-11 13:01:32 +08:00
// read contents after copy file
fmt.Println(gfile.GetContents(dstTempFile))
2021-11-05 14:30:50 +08:00
// copy dir
2021-11-11 13:01:32 +08:00
gfile.Copy(srcTempDir, dstTempDir)
2021-11-05 14:30:50 +08:00
2021-11-11 13:01:32 +08:00
// list copy dir file
2021-11-05 14:30:50 +08:00
fList, _ := gfile.ScanDir(dstTempDir, "*", false)
for _, v := range fList {
2021-11-11 13:01:32 +08:00
fmt.Println(gfile.Basename(v))
2021-11-05 14:30:50 +08:00
}
// Output:
2021-11-11 13:01:32 +08:00
// goframe example copy
// gflie_example.txt
// gflie_example_copy.txt
2021-11-05 14:30:50 +08:00
}