drwxr-xr-x
。
func Mkdir(path string) error
func ExampleMkdir() {
// init
var (
path = gfile.TempDir("gfile_example_basic_dir")
)
// Creates directory
gfile.Mkdir(path)
// Check if directory exists
fmt.Println(gfile.IsDir(path))
// Output:
// true
}
-rw-r–r–
。
func Create(path string) (*os.File, error)
func ExampleCreate() {
// init
var (
path = gfile.Join(gfile.TempDir("gfile_example_basic_dir"), "file1")
dataByte = make([]byte, 50)
)
// Check whether the file exists
isFile := gfile.IsFile(path)
fmt.Println(isFile)
// Creates file with given `path` recursively
fileHandle, _ := gfile.Create(path)
defer fileHandle.Close()
// Write some content to file
n, _ := fileHandle.WriteString("hello goframe")
// Check whether the file exists
isFile = gfile.IsFile(path)
fmt.Println(isFile)
// Reset file uintptr
unix.Seek(int(fileHandle.Fd()), 0, 0)
// Reads len(b) bytes from the File
fileHandle.Read(dataByte)
fmt.Println(string(dataByte[:n]))
// Output:
// false
// true
// hello goframe
}
func Open(path string) (*os.File, error)
func ExampleOpen() {
// init
var (
path = gfile.Join(gfile.TempDir("gfile_example_basic_dir"), "file1")
dataByte = make([]byte, 4096)
)
// Open file or directory with READONLY model
file, _ := gfile.Open(path)
defer file.Close()
// Read data
n, _ := file.Read(dataByte)
fmt.Println(string(dataByte[:n]))
// Output:
// hello goframe
}
func OpenFile(path string, flag int, perm os.FileMode) (*os.File, error)
func ExampleOpenFile() {
// init
var (
path = gfile.Join(gfile.TempDir("gfile_example_basic_dir"), "file1")
dataByte = make([]byte, 4096)
)
// Opens file/directory with custom `flag` and `perm`
// Create if file does not exist,it is created in a readable and writable mode,prem 0777
openFile, _ := gfile.OpenFile(path, os.O_CREATE|os.O_RDWR, gfile.DefaultPermCopy)
defer openFile.Close()
// Write some content to file
writeLength, _ := openFile.WriteString("hello goframe test open file")
fmt.Println(writeLength)
// Read data
unix.Seek(int(openFile.Fd()), 0, 0)
n, _ := openFile.Read(dataByte)
fmt.Println(string(dataByte[:n]))
// Output:
// 28
// hello goframe test open file
}
func OpenWithFlag(path string, flag int) (*os.File, error)
func ExampleOpenWithFlag() {
// init
var (
path = gfile.Join(gfile.TempDir("gfile_example_basic_dir"), "file1")
dataByte = make([]byte, 4096)
)
// Opens file/directory with custom `flag`
// Create if file does not exist,it is created in a readable and writable mode with default `perm` is 0666
openFile, _ := gfile.OpenWithFlag(path, os.O_CREATE|os.O_RDWR)
defer openFile.Close()
// Write some content to file
writeLength, _ := openFile.WriteString("hello goframe test open file with flag")
fmt.Println(writeLength)
// Read data
unix.Seek(int(openFile.Fd()), 0, 0)
n, _ := openFile.Read(dataByte)
fmt.Println(string(dataByte[:n]))
// Output:
// 38
// hello goframe test open file with flag
}
func OpenWithFlagPerm(path string, flag int, perm os.FileMode) (*os.File, error)
func ExampleOpenWithFlagPerm() {
// init
var (
path = gfile.Join(gfile.TempDir("gfile_example_basic_dir"), "file1")
dataByte = make([]byte, 4096)
)
// Opens file/directory with custom `flag` and `perm`
// Create if file does not exist,it is created in a readable and writable mode with `perm` is 0777
openFile, _ := gfile.OpenWithFlagPerm(path, os.O_CREATE|os.O_RDWR, gfile.DefaultPermCopy)
defer openFile.Close()
// Write some content to file
writeLength, _ := openFile.WriteString("hello goframe test open file with flag and perm")
fmt.Println(writeLength)
// Read data
unix.Seek(int(openFile.Fd()), 0, 0)
n, _ := openFile.Read(dataByte)
fmt.Println(string(dataByte[:n]))
// Output:
// 38
// hello goframe test open file with flag
}
func Stat(path string) (os.FileInfo, error)
func ExampleStat() {
// init
var (
path = gfile.Join(gfile.TempDir("gfile_example_basic_dir"), "file1")
)
// Get a FileInfo describing the named file.
stat, _ := gfile.Stat(path)
fmt.Println(stat.Name())
fmt.Println(stat.IsDir())
fmt.Println(stat.Mode())
fmt.Println(stat.ModTime())
fmt.Println(stat.Size())
fmt.Println(stat.Sys())
// May Output:
// file1
// false
// -rwxr-xr-x
// 2021-12-02 11:01:27.261441694 +0800 CST
// &{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]}
}
func Copy(src string, dst string) error
func ExampleCopy() {
// init
var (
srcFileName = "gflie_example.txt"
srcTempDir = gfile.TempDir("gfile_example_copy_src")
srcTempFile = gfile.Join(srcTempDir, srcFileName)
// copy file
dstFileName = "gflie_example_copy.txt"
dstTempFile = gfile.Join(srcTempDir, dstFileName)
// copy dir
dstTempDir = gfile.TempDir("gfile_example_copy_dst")
)
// write contents
gfile.PutContents(srcTempFile, "goframe example copy")
// copy file
gfile.Copy(srcTempFile, dstTempFile)
// read contents after copy file
fmt.Println(gfile.GetContents(dstTempFile))
// copy dir
gfile.Copy(srcTempDir, dstTempDir)
// list copy dir file
fList, _ := gfile.ScanDir(dstTempDir, "*", false)
for _, v := range fList {
fmt.Println(gfile.Basename(v))
}
// Output:
// goframe example copy
// gflie_example.txt
// gflie_example_copy.txt
}
func CopyFile(src, dst string) (err error)
func ExampleCopyFile() {
// init
var (
srcFileName = "gflie_example.txt"
srcTempDir = gfile.TempDir("gfile_example_copy_src")
srcTempFile = gfile.Join(srcTempDir, srcFileName)
// copy file
dstFileName = "gflie_example_copy.txt"
dstTempFile = gfile.Join(srcTempDir, dstFileName)
)
// write contents
gfile.PutContents(srcTempFile, "goframe example copy")
// copy file
gfile.CopyFile(srcTempFile, dstTempFile)
// read contents after copy file
fmt.Println(gfile.GetContents(dstTempFile))
// Output:
// goframe example copy
}
func CopyDir(src string, dst string) error
func ExampleCopyDir() {
// init
var (
srcTempDir = gfile.TempDir("gfile_example_copy_src")
// copy file
dstFileName = "gflie_example_copy.txt"
dstTempFile = gfile.Join(srcTempDir, dstFileName)
// copy dir
dstTempDir = gfile.TempDir("gfile_example_copy_dst")
)
// read contents after copy file
fmt.Println(gfile.GetContents(dstTempFile))
// copy dir
gfile.CopyDir(srcTempDir, dstTempDir)
// list copy dir file
fList, _ := gfile.ScanDir(dstTempDir, "*", false)
for _, v := range fList {
fmt.Println(gfile.Basename(v))
}
// Output:
// gflie_example.txt
// gflie_example_copy.txt
}
src
重命名为dst
。
dst
已经存在并且是文件,将会被替换造成数据丢失!
func Move(src string, dst string) error
func ExampleMove() {
// init
var (
srcPath = gfile.Join(gfile.TempDir("gfile_example_basic_dir"), "file1")
dstPath = gfile.Join(gfile.TempDir("gfile_example_basic_dir"), "file2")
)
// Check is file
fmt.Println(gfile.IsFile(dstPath))
// Moves `src` to `dst` path.
// If `dst` already exists and is not a directory, it"ll be replaced.
gfile.Move(srcPath, dstPath)
fmt.Println(gfile.IsFile(srcPath))
fmt.Println(gfile.IsFile(dstPath))
// Output:
// false
// false
// true
}
Move
的别名,将src
重命名为dst
。
dst
已经存在并且是文件,将会被替换造成数据丢失!
func Rename(src string, dst string) error
func ExampleRename() {
// init
var (
srcPath = gfile.Join(gfile.TempDir("gfile_example_basic_dir"), "file2")
dstPath = gfile.Join(gfile.TempDir("gfile_example_basic_dir"), "file1")
)
// Check is file
fmt.Println(gfile.IsFile(dstPath))
// renames (moves) `src` to `dst` path.
// If `dst` already exists and is not a directory, it"ll be replaced.
gfile.Rename(srcPath, dstPath)
fmt.Println(gfile.IsFile(srcPath))
fmt.Println(gfile.IsFile(dstPath))
// Output:
// false
// false
// true
}
func Remove(path string) error
func ExampleRemove() {
// init
var (
path = gfile.Join(gfile.TempDir("gfile_example_basic_dir"), "file1")
)
// Checks whether given `path` a file, which means it"s not a directory.
fmt.Println(gfile.IsFile(path))
// deletes all file/directory with `path` parameter.
gfile.Remove(path)
// Check again
fmt.Println(gfile.IsFile(path))
// Output:
// true
// false
}
func IsEmpty(path string) bool
func ExampleIsEmpty() {
// init
var (
path = gfile.Join(gfile.TempDir("gfile_example_basic_dir"), "file1")
)
// Check whether the `path` is empty
fmt.Println(gfile.IsEmpty(path))
// Truncate file
gfile.Truncate(path, 0)
// Check whether the `path` is empty
fmt.Println(gfile.IsEmpty(path))
// Output:
// false
// true
}
func DirNames(path string) ([]string, error)
func ExampleDirNames() {
// init
var (
path = gfile.TempDir("gfile_example_basic_dir")
)
// Get sub-file names of given directory `path`.
dirNames, _ := gfile.DirNames(path)
fmt.Println(dirNames)
// May Output:
// [file1]
}
func Glob(pattern string, onlyNames ...bool) ([]string, error)
func ExampleGlob() {
// init
var (
path = gfile.Pwd() + gfile.Separator + "*_example_basic_test.go"
)
// Get sub-file names of given directory `path`.
// Only show file name
matchNames, _ := gfile.Glob(path, true)
fmt.Println(matchNames)
// Show full path of the file
matchNames, _ = gfile.Glob(path, false)
fmt.Println(matchNames)
// May Output:
// [gfile_z_example_basic_test.go]
// [xxx/gf/os/gfile/gfile_z_example_basic_test.go]
}
func Exists(path string) bool
func ExampleExists() {
// init
var (
path = gfile.Join(gfile.TempDir("gfile_example_basic_dir"), "file1")
)
// Checks whether given `path` exist.
fmt.Println(gfile.Exists(path))
// Output:
// true
}
func Chdir(dir string) error
func ExampleChdir() {
// init
var (
path = gfile.Join(gfile.TempDir("gfile_example_basic_dir"), "file1")
)
// Get current working directory
fmt.Println(gfile.Pwd())
// Changes the current working directory to the named directory.
gfile.Chdir(path)
// Get current working directory
fmt.Println(gfile.Pwd())
// May Output:
// xxx/gf/os/gfile
// /tmp/gfile_example_basic_dir/file1
}
Pos说明:Pos返回needle在haystack中第一次出现的位置,区分大小写。如果没有找到,则返回-1。格式:Pos(haystack, need...
基本介绍gjson模块实现了强大的数据编码/解析功能,支持数据层级检索、动态创建修改Json对象,并支持常见数据格式的解析和转...
GoFrame框架提供了强大的字符编码转换模块gchatset,支持常见字符集的相互转换。支持的字符集:编码字符集中文GBK/GB1803...