Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

基本介绍

gfile文件管理组件提供了更加丰富的文件/目录操作能力。

使用方式

import "github.com/gogf/gf/v2/os/gfile"

接口文档

https://pkg.go.dev/github.com/gogf/gf/v2/os/gfile

内容管理

GetContents

  • 说明:读取指定路径文件内容,以字符串形式返回。
  • 示例:

    Code Block
    languagego
    func ExampleGetContents() {
    	// init
    	var (
    		fileName = "gflie_example.txt"
    		tempDir  = gfile.TempDir("gfile_example_content")
    		tempFile = gfile.Join(tempDir, fileName)
    	)
    
    	// write contents
    	gfile.PutContents(tempFile, "goframe example content")
    
    	// It reads and returns the file content as string.
    	// It returns empty string if it fails reading, for example, with permission or IO error.
    	fmt.Println(gfile.GetContents(tempFile))
    
    	// Output:
    	// goframe example content
    }


GetContentsWithCache

    • 说明:带缓存获取文件内容,可设置缓存超时,文件发生变化自动清除缓存。
    • 示例:

      Code Block
      languagego
      func ExampleGetContentsWithCache() {
      	// init
      	var (
      		fileName = "gflie_example.txt"
      		tempDir  = gfile.TempDir("gfile_example_cache")
      		tempFile = gfile.Join(tempDir, fileName)
      	)
      
      	// write contents
      	gfile.PutContents(tempFile, "goframe example content")
      
      	// It reads the file content with cache duration of one minute,
      	// which means it reads from cache after then without any IO operations within on minute.
      	fmt.Println(gfile.GetContentsWithCache(tempFile, time.Minute))
      
      	// write new contents will clear its cache
      	gfile.PutContents(tempFile, "new goframe example content")
      
      	// There's some delay for cache clearing after file content change.
      	time.Sleep(time.Second * 1)
      
      	// read contents
      	fmt.Println(gfile.GetContentsWithCache(tempFile))
      
      	// May Output:
      	// goframe example content
      	// new goframe example content
      }


GetBytes

  • 说明:读取指定路径文件内容,以字节形式返回。
  • 示例:

    Code Block
    languagego
    func ExampleGetBytes() {
    	// init
    	var (
    		fileName = "gflie_example.txt"
    		tempDir  = gfile.TempDir("gfile_example_content")
    		tempFile = gfile.Join(tempDir, fileName)
    	)
    
    	// write contents
    	gfile.PutContents(tempFile, "goframe example content")
    
    	// It reads and returns the file content as []byte.
    	// It returns nil if it fails reading, for example, with permission or IO error.
    	fmt.Println(gfile.GetBytes(tempFile))
    
    	// Output:
    	// [103 111 102 114 97 109 101 32 101 120 97 109 112 108 101 32 99 111 110 116 101 110 116]
    }


PutContents

  • 说明:往指定路径文件添加字符串内容。如果文件不存在将会递归的形式自动创建。
  • 示例:

    Code Block
    languagego
    func ExamplePutContents() {
    	// init
    	var (
    		fileName = "gflie_example.txt"
    		tempDir  = gfile.TempDir("gfile_example_content")
    		tempFile = gfile.Join(tempDir, fileName)
    	)
    
    	// It creates and puts content string into specifies file path.
    	// It automatically creates directory recursively if it does not exist.
    	gfile.PutContents(tempFile, "goframe example content")
    
    	// read contents
    	fmt.Println(gfile.GetContents(tempFile))
    
    	// Output:
    	// goframe example content
    }


PutBytes

  • 说明:以字节形式写入指定文件,如果文件不存在将会递归的形式自动创建
  • 示例:

    Code Block
    languagego
    func ExamplePutBytes() {
    	// init
    	var (
    		fileName = "gflie_example.txt"
    		tempDir  = gfile.TempDir("gfile_example_content")
    		tempFile = gfile.Join(tempDir, fileName)
    	)
    
    	// write contents
    	gfile.PutBytes(tempFile, []byte("goframe example content"))
    
    	// read contents
    	fmt.Println(gfile.GetContents(tempFile))
    
    	// Output:
    	// goframe example content
    }


PutContentsAppend

  • 说明:追加字符串内容到指定文件,如果文件不存在将会递归的形式自动创建。
  • 示例:

    Code Block
    languagego
    func ExamplePutContentsAppend() {
    	// init
    	var (
    		fileName = "gflie_example.txt"
    		tempDir  = gfile.TempDir("gfile_example_content")
    		tempFile = gfile.Join(tempDir, fileName)
    	)
    
    	// write contents
    	gfile.PutContents(tempFile, "goframe example content")
    
    	// read contents
    	fmt.Println(gfile.GetContents(tempFile))
    
    	// It creates and append content string into specifies file path.
    	// It automatically creates directory recursively if it does not exist.
    	gfile.PutContentsAppend(tempFile, " append content")
    
    	// read contents
    	fmt.Println(gfile.GetContents(tempFile))
    
    	// Output:
    	// goframe example content
    	// goframe example content append content
    }


PutBytesAppend

  • 说明:追加字节内容到指定文件。如果文件不存在将会递归的形式自动创建。
  • 示例:

    Code Block
    languagego
    func ExamplePutBytesAppend() {
    	// init
    	var (
    		fileName = "gflie_example.txt"
    		tempDir  = gfile.TempDir("gfile_example_content")
    		tempFile = gfile.Join(tempDir, fileName)
    	)
    
    	// write contents
    	gfile.PutContents(tempFile, "goframe example content")
    
    	// read contents
    	fmt.Println(gfile.GetContents(tempFile))
    
    	// write contents
    	gfile.PutBytesAppend(tempFile, []byte(" append"))
    
    	// read contents
    	fmt.Println(gfile.GetContents(tempFile))
    
    	// Output:
    	// goframe example content
    	// goframe example content append
    }


GetNextCharOffsetByPath

  • 说明:从某个偏移量开始,获取文件中指定字符所在下标
  • 示例:

    Code Block
    languagego
    func ExampleGetNextCharOffsetByPath() {
    	// init
    	var (
    		fileName = "gflie_example.txt"
    		tempDir  = gfile.TempDir("gfile_example_content")
    		tempFile = gfile.Join(tempDir, fileName)
    	)
    
    	// write contents
    	gfile.PutContents(tempFile, "goframe example content")
    
    	// read contents
    	index := gfile.GetNextCharOffsetByPath(tempFile, 'f', 0)
    	fmt.Println(index)
    
    	// Output:
    	// 2
    }


GetBytesTilCharByPath

  • 说明:以某个字符定位截取指定长度的文件内容以字节形式返回
  • 示例:

    Code Block
    languagego
    func ExampleGetBytesTilCharByPath() {
    	// init
    	var (
    		fileName = "gflie_example.txt"
    		tempDir  = gfile.TempDir("gfile_example_content")
    		tempFile = gfile.Join(tempDir, fileName)
    	)
    
    	// write contents
    	gfile.PutContents(tempFile, "goframe example content")
    
    	// read contents
    	fmt.Println(gfile.GetBytesTilCharByPath(tempFile, 'f', 0))
    
    	// Output:
    	// [103 111 102] 2
    }


GetBytesByTwoOffsetsByPath

  • 说明:用两个偏移量截取指定文件的内容以字节形式返回
  • 示例:

    Code Block
    languagego
    func ExampleGetBytesByTwoOffsetsByPath() {
    	// init
    	var (
    		fileName = "gflie_example.txt"
    		tempDir  = gfile.TempDir("gfile_example_content")
    		tempFile = gfile.Join(tempDir, fileName)
    	)
    
    	// write contents
    	gfile.PutContents(tempFile, "goframe example content")
    
    	// read contents
    	fmt.Println(gfile.GetBytesByTwoOffsetsByPath(tempFile, 0, 7))
    
    	// Output:
    	// [103 111 102 114 97 109 101]
    }


ReadLines

  • 说明:以字符串形式逐行读取文件内容
  • 示例:

    Code Block
    languagego
    func ExampleReadLines() {
    	// init
    	var (
    		fileName = "gflie_example.txt"
    		tempDir  = gfile.TempDir("gfile_example_content")
    		tempFile = gfile.Join(tempDir, fileName)
    	)
    
    	// write contents
    	gfile.PutContents(tempFile, "L1 goframe example content\nL2 goframe example content")
    
    	// read contents
    	gfile.ReadLines(tempFile, func(text string) error {
    		// Process each line
    		fmt.Println(text)
    		return nil
    	})
    
    	// Output:
    	// L1 goframe example content
    	// L2 goframe example content
    }


ReadLinesBytes

  • 说明:以字节形式逐行读取文件内容
  • 示例:

    Code Block
    languagego
    func ExampleReadLinesBytes() {
    	// init
    	var (
    		fileName = "gflie_example.txt"
    		tempDir  = gfile.TempDir("gfile_example_content")
    		tempFile = gfile.Join(tempDir, fileName)
    	)
    
    	// write contents
    	gfile.PutContents(tempFile, "L1 goframe example content\nL2 goframe example content")
    
    	// read contents
    	gfile.ReadLinesBytes(tempFile, func(bytes []byte) error {
    		// Process each line
    		fmt.Println(bytes)
    		return nil
    	})
    
    	// Output:
    	// [76 49 32 103 111 102 114 97 109 101 32 101 120 97 109 112 108 101 32 99 111 110 116 101 110 116]
    	// [76 50 32 103 111 102 114 97 109 101 32 101 120 97 109 112 108 101 32 99 111 110 116 101 110 116]
    }


内容替换

ReplaceFile

  • 说明:替换指定文件的指定内容为新内容
  • 示例:

    Code Block
    languagego
    func ExampleReplaceFile() {
    	// init
    	var (
    		fileName = "gflie_example.txt"
    		tempDir  = gfile.TempDir("gfile_example_replace")
    		tempFile = gfile.Join(tempDir, fileName)
    	)
    
    	// write contents
    	gfile.PutContents(tempFile, "goframe example content")
    
    	// read contents
    	fmt.Println(gfile.GetContents(tempFile))
    
    	// It replaces content directly by file path.
    	gfile.ReplaceFile("content", "replace word", tempFile)
    
    	fmt.Println(gfile.GetContents(tempFile))
    
    	// Output:
    	// goframe example content
    	// goframe example replace word
    }


ReplaceFileFunc

  • 说明:使用自定义函数替换指定文件内容
  • 示例:

    Code Block
    languagego
    func ExampleReplaceFileFunc() {
    	// init
    	var (
    		fileName = "gflie_example.txt"
    		tempDir  = gfile.TempDir("gfile_example_replace")
    		tempFile = gfile.Join(tempDir, fileName)
    	)
    
    	// write contents
    	gfile.PutContents(tempFile, "goframe example 123")
    
    	// read contents
    	fmt.Println(gfile.GetContents(tempFile))
    
    	// It replaces content directly by file path and callback function.
    	gfile.ReplaceFileFunc(func(path, content string) string {
    		// Replace with regular match
    		reg, _ := regexp.Compile(`\d{3}`)
    		return reg.ReplaceAllString(content, "[num]")
    	}, tempFile)
    
    	fmt.Println(gfile.GetContents(tempFile))
    
    	// Output:
    	// goframe example 123
    	// goframe example [num]
    }


ReplaceDir

  • 说明:扫描指定目录,替换符合条件的文件的指定内容为新内容
  • 示例:

    Code Block
    languagego
    func ExampleReplaceDir() {
    	// init
    	var (
    		fileName = "gflie_example.txt"
    		tempDir  = gfile.TempDir("gfile_example_replace")
    		tempFile = gfile.Join(tempDir, fileName)
    	)
    
    	// write contents
    	gfile.PutContents(tempFile, "goframe example content")
    
    	// read contents
    	fmt.Println(gfile.GetContents(tempFile))
    
    	// It replaces content of all files under specified directory recursively.
    	gfile.ReplaceDir("content", "replace word", tempDir, "gflie_example.txt", true)
    
    	// read contents
    	fmt.Println(gfile.GetContents(tempFile))
    
    	// Output:
    	// goframe example content
    	// goframe example replace word
    }


ReplaceDirFunc

  • 说明:扫描指定目录,使用自定义函数替换符合条件的文件的指定内容为新内容
  • 示例:

    Code Block
    languagego
    func ExampleReplaceDirFunc() {
    	// init
    	var (
    		fileName = "gflie_example.txt"
    		tempDir  = gfile.TempDir("gfile_example_replace")
    		tempFile = gfile.Join(tempDir, fileName)
    	)
    
    	// write contents
    	gfile.PutContents(tempFile, "goframe example 123")
    
    	// read contents
    	fmt.Println(gfile.GetContents(tempFile))
    
    	// It replaces content of all files under specified directory with custom callback function recursively.
    	gfile.ReplaceDirFunc(func(path, content string) string {
    		// Replace with regular match
    		reg, _ := regexp.Compile(`\d{3}`)
    		return reg.ReplaceAllString(content, "[num]")
    	}, tempDir, "gflie_example.txt", true)
    
    	fmt.Println(gfile.GetContents(tempFile))
    
    	// Output:
    	// goframe example 123
    	// goframe example [num]
    
    }


文件时间

MTime

  • 说明:获取路径修改时间
  • 示例:

    Code Block
    languagego
    func ExampleMTime() {
    	t := gfile.MTime(gfile.TempDir())
    	fmt.Println(t)
    
    	// May Output:
    	// 2021-11-02 15:18:43.901141 +0800 CST
    }


MTimestamp

  • 说明:获取路径修改时间戳(秒)
  • 示例:

    Code Block
    languagego
    func ExampleMTimestamp() {
    	t := gfile.MTimestamp(gfile.TempDir())
    	fmt.Println(t)
    
    	// May Output:
    	// 1635838398
    }


MTimestampMilli

  • 说明:获取路径修改时间戳(毫秒)
  • 示例:

    Code Block
    languagego
    func ExampleMTimestampMilli() {
    	t := gfile.MTimestampMilli(gfile.TempDir())
    	fmt.Println(t)
    
    	// May Output:
    	// 1635838529330
    }


文件大小

Size

  • 说明:获取路径大小,不进行格式化
  • 示例:

    Code Block
    languagego
    func ExampleSize() {
    	// init
    	var (
    		fileName = "gflie_example.txt"
    		tempDir  = gfile.TempDir("gfile_example_size")
    		tempFile = gfile.Join(tempDir, fileName)
    	)
    
    	// write contents
    	gfile.PutContents(tempFile, "0123456789")
    	fmt.Println(gfile.Size(tempFile))
    
    	// Output:
    	// 10
    }


SizeFormat

  • 说明:获取路径大小,并格式化成硬盘容量
  • 示例:

    Code Block
    languagego
    func ExampleSizeFormat() {
    	// init
    	var (
    		fileName = "gflie_example.txt"
    		tempDir  = gfile.TempDir("gfile_example_size")
    		tempFile = gfile.Join(tempDir, fileName)
    	)
    
    	// write contents
    	gfile.PutContents(tempFile, "0123456789")
    	fmt.Println(gfile.SizeFormat(tempFile))
    
    	// Output:
    	// 10.00B
    }


ReadableSize

  • 说明:获取给定路径容量大小,并格式化人类易读的硬盘容量格式
  • 示例:

    Code Block
    languagego
    func ExampleReadableSize() {
    	// init
    	var (
    		fileName = "gflie_example.txt"
    		tempDir  = gfile.TempDir("gfile_example_size")
    		tempFile = gfile.Join(tempDir, fileName)
    	)
    
    	// write contents
    	gfile.PutContents(tempFile, "01234567899876543210")
    	fmt.Println(gfile.ReadableSize(tempFile))
    
    	// Output:
    	// 20.00B
    }


StrToSize

  • 说明:硬盘容量大小字符串转换为大小整形
  • 示例:

    Code Block
    languagego
    func ExampleStrToSize() {
    	size := gfile.StrToSize("100MB")
    	fmt.Println(size)
    
    	// Output:
    	// 104857600
    }


FormatSize

  • 说明:大小整形转换为硬盘容量大小字符串`K、m、g、t、p、e、b`
  • 示例:

    Code Block
    languagego
    func ExampleFormatSize() {
    	sizeStr := gfile.FormatSize(104857600)
    	fmt.Println(sizeStr)
    	sizeStr0 := gfile.FormatSize(1024)
    	fmt.Println(sizeStr0)
    	sizeStr1 := gfile.FormatSize(999999999999999999)
    	fmt.Println(sizeStr1)
    
    	// Output:
    	// 100.00M
    	// 1.00K
    	// 888.18P
    }


文件排序

SortFiles

  • 说明:排序多个路径,按首字母进行排序,数字优先。
  • 示例:

    Code Block
    languagego
    func ExampleSortFiles() {
    	files := []string{
    		"/aaa/bbb/ccc.txt",
    		"/aaa/bbb/",
    		"/aaa/",
    		"/aaa",
    		"/aaa/ccc/ddd.txt",
    		"/bbb",
    		"/0123",
    		"/ddd",
    		"/ccc",
    	}
    	sortOut := gfile.SortFiles(files)
    	fmt.Println(sortOut)
    
    	// Output:
    	// [/0123 /aaa /aaa/ /aaa/bbb/ /aaa/bbb/ccc.txt /aaa/ccc/ddd.txt /bbb /ccc /ddd]
    }


文件检索

  • 说明:在指定目录(默认包含当前目录、运行目录、主函数目录;不会递归子目录)中搜索文件并返回真实路径。
  • 示例:

    Code Block
    languagego
    func ExampleSearch() {
    	// init
    	var (
    		fileName = "gflie_example.txt"
    		tempDir  = gfile.TempDir("gfile_example_search")
    		tempFile = gfile.Join(tempDir, fileName)
    	)
    
    	// write contents
    	gfile.PutContents(tempFile, "goframe example content")
    
    	// search file
    	realPath, _ := gfile.Search(fileName, tempDir)
    	fmt.Println(gfile.Basename(realPath))
    
    	// Output:
    	// gflie_example.txt
    }


目录扫描

ScanDir

  • 说明:扫描指定目录,可扫描文件或目录,支持递归扫描。
  • 示例:

    Code Block
    languagego
    func ExampleScanDir() {
    	// init
    	var (
    		fileName = "gflie_example.txt"
    		tempDir  = gfile.TempDir("gfile_example_scan_dir")
    		tempFile = gfile.Join(tempDir, fileName)
    
    		tempSubDir  = gfile.Join(tempDir, "sub_dir")
    		tempSubFile = gfile.Join(tempSubDir, fileName)
    	)
    
    	// write contents
    	gfile.PutContents(tempFile, "goframe example content")
    	gfile.PutContents(tempSubFile, "goframe example content")
    
    	// scans directory recursively
    	list, _ := gfile.ScanDir(tempDir, "*", true)
    	for _, v := range list {
    		fmt.Println(gfile.Basename(v))
    	}
    
    	// Output:
    	// gflie_example.txt
    	// sub_dir
    	// gflie_example.txt
    }


ScanDirFile

  • 说明:扫描指定目录的文件,支持递归扫描
  • 示例:

    Code Block
    languagego
    func ExampleScanDirFile() {
    	// init
    	var (
    		fileName = "gflie_example.txt"
    		tempDir  = gfile.TempDir("gfile_example_scan_dir_file")
    		tempFile = gfile.Join(tempDir, fileName)
    
    		tempSubDir  = gfile.Join(tempDir, "sub_dir")
    		tempSubFile = gfile.Join(tempSubDir, fileName)
    	)
    
    	// write contents
    	gfile.PutContents(tempFile, "goframe example content")
    	gfile.PutContents(tempSubFile, "goframe example content")
    
    	// scans directory recursively exclusive of directories
    	list, _ := gfile.ScanDirFile(tempDir, "*.txt", true)
    	for _, v := range list {
    		fmt.Println(gfile.Basename(v))
    	}
    
    	// Output:
    	// gflie_example.txt
    	// gflie_example.txt
    }


ScanDirFunc

  • 说明:扫描指定目录(自定义过滤方法),可扫描文件或目录,支持递归扫描
  • 示例:

    Code Block
    languagego
    func ExampleScanDirFunc() {
    	// init
    	var (
    		fileName = "gflie_example.txt"
    		tempDir  = gfile.TempDir("gfile_example_scan_dir_func")
    		tempFile = gfile.Join(tempDir, fileName)
    
    		tempSubDir  = gfile.Join(tempDir, "sub_dir")
    		tempSubFile = gfile.Join(tempSubDir, fileName)
    	)
    
    	// write contents
    	gfile.PutContents(tempFile, "goframe example content")
    	gfile.PutContents(tempSubFile, "goframe example content")
    
    	// scans directory recursively
    	list, _ := gfile.ScanDirFunc(tempDir, "*", true, func(path string) string {
    		// ignores some files
    		if gfile.Basename(path) == "gflie_example.txt" {
    			return ""
    		}
    		return path
    	})
    	for _, v := range list {
    		fmt.Println(gfile.Basename(v))
    	}
    
    	// Output:
    	// sub_dir
    }


ScanDirFileFunc

  • 说明:扫描指定目录的文件(自定义过滤方法),支持递归扫描。
  • 示例:

    Code Block
    languagego
    func ExampleScanDirFileFunc() {
    	// init
    	var (
    		fileName = "gflie_example.txt"
    		tempDir  = gfile.TempDir("gfile_example_scan_dir_file_func")
    		tempFile = gfile.Join(tempDir, fileName)
    
    		fileName1 = "gflie_example_ignores.txt"
    		tempFile1 = gfile.Join(tempDir, fileName1)
    
    		tempSubDir  = gfile.Join(tempDir, "sub_dir")
    		tempSubFile = gfile.Join(tempSubDir, fileName)
    	)
    
    	// write contents
    	gfile.PutContents(tempFile, "goframe example content")
    	gfile.PutContents(tempFile1, "goframe example content")
    	gfile.PutContents(tempSubFile, "goframe example content")
    
    	// scans directory recursively exclusive of directories
    	list, _ := gfile.ScanDirFileFunc(tempDir, "*.txt", true, func(path string) string {
    		// ignores some files
    		if gfile.Basename(path) == "gflie_example_ignores.txt" {
    			return ""
    		}
    		return path
    	})
    	for _, v := range list {
    		fmt.Println(gfile.Basename(v))
    	}
    
    	// Output:
    	// gflie_example.txt
    	// gflie_example.txt
    }
文件/目录操作

常用目录

Copy

Home

  • 说明:支持复制文件或目录说明:获取运行用户的主目录
  • 示例:

    Code Block
    languagego
    func ExampleCopyExampleHome() {
    	// init
    	var (
    		srcFileName = "gflie_example.txt"
    		srcTempDir   user's home directory
    	homePath, _ := gfile.TempDir("gfile_example_copy_src"Home()
    		srcTempFile = gfile.Join(srcTempDir, srcFileNamefmt.Println(homePath)
    
    		// copyMay fileOutput:
    		dstFileName = "gflie_example_copy.txt"
    		dstTempFile = gfile.Join(srcTempDir, dstFileName)
    
    		// copy dir
    		dstTempDir = gfile.TempDir(// C:\Users\hailaz
    }


TempDir

  • 说明:获取拼接系统临时路径后的绝对地址。

  • 示例:

    Code Block
    languagego
    func ExampleTempDir() {
    	// init
    	var (
    		fileName = "gfile_example_
  • copy
  • basic_
  • dst
  • dir"
  • )
  • 
    	)
    
    	// 
  • write contents gfile.PutContents(srcTempFile, "goframe example copy") // copy file gfile.Copy(srcTempFile, dstTempFile) // read contents after copy file fmt.Println(gfile.GetContents(dstTempFile)
  • fetch an absolute representation of path.
    	path := gfile.TempDir(fileName)
    
    	fmt.Println(path)
    
    	// 
  • copy dir
  • Output:
    	
  • gfile.Copy(srcTempDir, dstTempDir) // list copy dir file fList, _ := gfile.ScanDir(dstTempDir, "*", false) for _, v := range fList {
  • // /tmp/gfile_example_basic_dir
    }


Pwd

  • 说明:获取当前工作路径。
  • 示例:

    Code Block
    languagego
    func ExamplePwd() {
    	// Get absolute path of current working directory.
    	fmt.Println(gfile.
  • Basename
  • Pwd(
  • v
  • ))
  • }
  • 
    
    	// May Output:
    	// 
  • goframe example copy // gflie_example.txt // gflie_example_copy.txt
  • xxx/gf/os/gfile
    }
Home

SelfPath

  • 说明:获取运行用户的主目录

    说明:获取当前运行程序的绝对路径。

  • 示例:

    Code Block
    languagego
    func 
  • ExampleHome
  • ExampleSelfPath() {
    
    	//
  • user's home directory homePath, _ := gfile.Home()
  •  Get absolute file path of current running process
    	fmt.Println(gfile.SelfPath(
  • homePath
  • ))
    
    	// May Output:
    	// 
  • C:\Users\hailaz }

Mkdir

  • xxx/___github_com_gogf_gf_v2_os_gfile__ExampleSelfPath
    }


文件/目录操作

Copy

  • 说明:支持复制文件或目录说明:创建文件夹,支持递归创建(建议采用绝对路径),创建后的文件夹权限为:`drwxr-xr-x`。
  • 示例:

    Code Block
    languagego
    func 
  • ExampleMkdir
  • ExampleCopy() {
    	// init
    	var (
    		
  • path
  • srcFileName = 
  • gfile.TempDir(
  • "
  • gfile_example_basic_dir
  • gflie_example.txt"
    		srcTempDir  = gfile.TempDir("gfile_example_copy_src")
    		srcTempFile = gfile.Join(srcTempDir, srcFileName)
    
    		//
  • Creates directory gfile.Mkdir(path)
  •  copy file
    		dstFileName = "gflie_example_copy.txt"
    		dstTempFile = gfile.Join(srcTempDir, dstFileName)
    
    		// 
  • Check if directory exists fmt.Println(gfile.IsDir(path)
  • copy dir
    		dstTempDir = gfile.TempDir("gfile_example_copy_dst")
    	)
    
    	// 
  • Output: // true }

Create

  • 说明:创建文件/文件夹,如果传入的路径中的文件夹不存在,则会自动创建文件夹以及文件,其中创建的文件权限为`-rw-r–r–`。
  • 注意:如果需要创建文件的已存在,则会清空该文件的内容!
  • 示例:

    Code Block
    languagego
    func ExampleCreate() {
  • write contents
    	gfile.PutContents(srcTempFile, "goframe example copy")
    
    	// 
  • init
  • copy file
    	
  • var ( path = gfile.Join(gfile.TempDir("gfile_example_basic_dir"), "file1") dataByte = make([]byte, 50) )
  • gfile.Copy(srcTempFile, dstTempFile)
    
    	// read contents after copy file
    	fmt.Println(gfile.GetContents(dstTempFile))
    
    	// copy dir
    	gfile.Copy(srcTempDir, dstTempDir)
    
    	// 
  • Check
  • list 
  • whether
  • copy 
  • the
  • dir file
  • exists
  • 
    	
  • isFile
  • fList, _ := gfile.
  • IsFile(path
  • ScanDir(dstTempDir, "*", false)
    
  • fmt.Println(isFile) // Creates file with given `path` recursively fileHandle, _ := gfile.Create(path) defer fileHandle.Close()
  • 	for _, v := range fList {
    		fmt.Println(gfile.Basename(v))
    	}
    
    	// Output:
    	// goframe example copy
    	// gflie_example.txt
    	// gflie_example_copy.txt
    }


Mkdir

  • 说明:创建文件夹,支持递归创建(建议采用绝对路径),创建后的文件夹权限为:drwxr-xr-x
  • 示例:

    Code Block
    languagego
    func ExampleMkdir() {
    	// init
    	var (
    		pathWrite some content to file
    	n, _ := fileHandle.WriteString("hello goframe")
    
    	// Check whether the file exists
    	isFile = gfile.IsFile(path)
    
    	fmt.Println(isFile)
    	TempDir("gfile_example_basic_dir")
    	)
    
    	// Creates directory
    	gfile.Mkdir(path)
    
    	// Check Resetif filedirectory uintptrexists
    	unixfmt.SeekPrintln(int(fileHandlegfile.FdIsDir(path)), 0, 0)
    
    	// Reads len(b) bytes from the File
    	fileHandle.Read(dataByte)
    
    	fmt.Println(string(dataByte[:n]))
    
    	// Output:
    	// false
    	// true
    	// hello goframe
    }
Open

Create

  • 说明:创建文件/文件夹,如果传入的路径中的文件夹不存在,则会自动创建文件夹以及文件,其中创建的文件权限为-rw-r–r–
  • 注意:如果需要创建文件的已存在,则会清空该文件的内容!说明:以只读的方式打开文件/文件夹。
  • 示例:

    Code Block
    languagego
    func ExampleOpenExampleCreate() {
    	// init
    	var (
    		path     = gfile.Join(gfile.TempDir("gfile_example_basic_dir"), "file1")
    		dataByte = make([]byte, 409650)
    	)
    	// OpenCheck filewhether orthe directory with READONLY model
    	file, _file exists
    	isFile := gfile.OpenIsFile(path)
    
    	defer filefmt.ClosePrintln(isFile)
    
    	// Read data
    	n Creates file with given `path` recursively
    	fileHandle, _ := filegfile.ReadCreate(dataBytepath)
    
    	fmtdefer fileHandle.Println(string(dataByte[:n])Close()
    
    	// Output:
    	//  Write some content to file
    	n, _ := fileHandle.WriteString("hello goframe
    }

OpenFile

  • 说明:以指定`flag`以及`perm`的方式打开文件/文件夹。
  • 示例:

    Code Block
    languagego
    func ExampleOpenFile() {")
    
    	// init
    	var (
    		path     Check whether the file exists
    	isFile = gfile.Join(gfile.TempDir("gfile_example_basic_dir"), "file1")
    		dataByte = make([]byte, 4096)
    	)IsFile(path)
    
    	fmt.Println(isFile)
    	
    	// OpensReset file/directory with custom `flag` and `perm` uintptr
    	unix.Seek(int(fileHandle.Fd()), 0, 0)
    	// 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()Reads len(b) bytes from the File
    	fileHandle.Read(dataByte)
    
    	fmt.Println(string(dataByte[:n]))
    
    	// Write some contentOutput:
    	// false
    	// true
    	// hello goframe
    }


Open

  • 说明:以只读的方式打开文件/文件夹。
  • 示例:

    Code Block
    languagego
    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 to file
    	writeLength, _ := openFile.WriteString("hello goframe test open file")
    
    	fmt.Println(writeLength)
    
    	// Read data
    	unix.Seek(int(openFile.Fd()), 0, 0)
    	n, _ := openFilefile.Read(dataByte)
    
    	fmt.Println(string(dataByte[:n]))
    
    	// Output:
    	// 28
    	// hello goframe test open filehello goframe
    }
OpenWithFalg

OpenFile

  • 说明:以指定`flag`的方式打开文件说明:以指定`flag`以及`perm`的方式打开文件/文件夹。
  • 示例:

    Code Block
    languagego
    func ExampleOpenWithFlagExampleOpenFile() {
    	// 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 default `perm` is 0666,prem 0777
    	openFile, _ := gfile.OpenWithFlagOpenFile(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")
    
    	fmt.Println(writeLength)
    
    	// Read data
    	unix.Seek(int(openFile.Fd()), 0, 0)
    	n, _ := openFile.Read(dataByte)
    
    	fmt.Println(string(dataByte[:n]))
    
    	// Output:
    	// 3828
    	// hello goframe test open file with flag
    }
OpenWithFalgPerm

OpenWithFalg

  • 说明:以指定`flag`以及`perm`的方式打开文件说明:以指定`flag`的方式打开文件/文件夹。
  • 示例:

    Code Block
    languagego
    func ExampleOpenWithFlagPermExampleOpenWithFlag() {
    	// 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 default `perm` is 07770666
    	openFile, _ := gfile.OpenWithFlagPermOpenWithFlag(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
    }
Stat

OpenWithFalgPerm

  • 说明:获取给定路径的文件详情。说明:以指定`flag`以及`perm`的方式打开文件/文件夹。
  • 示例:

    Code Block
    languagego
    func ExampleStatExampleOpenWithFlagPerm() {
    	// init
    	var (
    		path     = gfile.Join(gfile.TempDir("gfile_example_basic_dir"), "file1")
    		dataByte = make([]byte, 4096)
    	)
    
    	// Get a FileInfo describing the named file.
    	statOpens 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.StatOpenWithFlagPerm(path)
    
    	fmt.Println(stat.Name())
    	fmt.Println(stat.IsDir())
    	fmt.Println(stat.Mode())
    	fmt.Println(stat.ModTime()), 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(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]}
    }

Move

  • 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
    }


Stat

  • 说明:获取给定路径的文件详情。
  • 说明:将`src`重命名为`dst`。

  • 注意:如果dst已经存在并且是文件,将会被替换造成数据丢失!
  • 示例:

    Code Block
    languagego
    func ExampleMoveExampleStat() {
    	// init
    	var (
    		srcPathpath = gfile.Join(gfile.TempDir("gfile_example_basic_dir"), "file1")
    		dstPath = gfile.Join(gfile.TempDir("gfile_example_basic_dir"), "file2")
    	)
    	// Check is file)
    	// 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(gfilestat.IsFileSys(dstPath))
    
    	//  Moves `src` to `dst` path.May Output:
    	// file1
    	// false
    	// -rwxr-xr-x
    	// 2021-12-02 11:01:27.261441694 +0800 CST
    	// 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
    }

Rename

  •  &{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]}
    }


Move

  • 说明:将src重命名为dst

  • 注意:如果dst已经存在并且是文件,将会被替换造成数据丢失!
  • 说明:Move的别名,将`src`重命名为`dst`。

  • 注意:如果dst已经存在并且是文件,将会被替换造成数据丢失!
  • 示例:

    Code Block
    languagego
    func ExampleRenameExampleMove() {
    	// init
    	var (
    		srcPath = gfile.Join(gfile.TempDir("gfile_example_basic_dir"), "file2file1")
    		dstPath = gfile.Join(gfile.TempDir("gfile_example_basic_dir"), "file1file2")
    	)
    	// Check is file
    	fmt.Println(gfile.IsFile(dstPath))
    
    	//  renames (moves)Moves `src` to `dst` path.
    	// If `dst` already exists and is not a directory, it'll be replaced.
    	gfile.RenameMove(srcPath, dstPath)
    
    	fmt.Println(gfile.IsFile(srcPath))
    	fmt.Println(gfile.IsFile(dstPath))
    
    	// Output:
    	// false
    	// false
    	// true
    }

Remove


Rename

  • 说明:Move的别名,将src重命名为dst

  • 注意:如果dst已经存在并且是文件,将会被替换造成数据丢失!说明:删除给定路径的文件或文件夹。
  • 示例:

    Code Block
    languagego
    func ExampleRemoveExampleRename() {
    	// init
    	var (
    		srcPath = gfile.Join(gfile.TempDir("gfile_example_basic_dir"), "file2")
    		pathdstPath = gfile.Join(gfile.TempDir("gfile_example_basic_dir"), "file1")
    	)
    	// Check is file
    	fmt.Println(gfile.IsFile(dstPath))
    
    	// Checks whether given `path` renames (moves) `src` to `dst` path.
    	// If `dst` already exists and is not a filedirectory, which means it'sll notbe a directoryreplaced.
    	fmt.Println(gfile.IsFile(path)Rename(srcPath, dstPath)
    
    	// deletes all file/directory with `path` parameter.
    	gfile.Remove(path)
    
    	// Check againfmt.Println(gfile.IsFile(srcPath))
    	fmt.Println(gfile.IsFile(pathdstPath))
    
    	// Output:
    	// truefalse
    	// false
    	// true
    }
IsEmpty

Remove

  • 说明:检查给定的路径,如果是文件夹则检查是否包含文件,如果是文件则检查`容量`是否为空。说明:删除给定路径的文件或文件夹。

  • 示例:

    Code Block
    languagego
    func ExampleIsEmptyExampleRemove() {
    	// init
    	var (
    		path = gfile.Join(gfile.TempDir("gfile_example_basic_dir"), "file1")
    	)
    
    	// CheckChecks whether thegiven `path` is empty a file, which means it's not a directory.
    	fmt.Println(gfile.IsEmptyIsFile(path))
    
    	// deletes Truncateall file/directory with `path` parameter.
    	gfile.TruncateRemove(path, 0)
    
    	// Check whether the `path` is emptyagain
    	fmt.Println(gfile.IsEmptyIsFile(path))
    
    	// Output:
    	// falsetrue
    	// truefalse
    }
Chmod

IsEmpty

  • 说明:使用指定的权限,更改指定路径的文件权限。说明:检查给定的路径,如果是文件夹则检查是否包含文件,如果是文件则检查文件大小是否为空。

  • 示例:

    Code Block
    languagego
    func ExampleChmodExampleIsEmpty() {
    	// init
    	var (
    		path = gfile.Join(gfile.TempDir("gfile_example_basic_dir"), "file1")
    	)
    
    	// Get a FileInfo describing the named file.
    	stat, err := gfile.Stat(path)
    	if err != nil {
    		fmt.Println(err.Error())
    	}
    	// Show original mode	var (
    		path = gfile.Join(gfile.TempDir("gfile_example_basic_dir"), "file1")
    	)
    
    	// Check whether the `path` is empty
    	fmt.Println(statgfile.ModeIsEmpty(path))
    
    	// ChangeTruncate file model
    	gfile.ChmodTruncate(path, gfile.DefaultPermCopy0)
    
    	// Get a FileInfo describingCheck whether the named file.
    	stat, _ = gfile.Stat(path)
    	// Show the modified mode
    	`path` is empty
    	fmt.Println(statgfile.ModeIsEmpty(path))
    
    	// Output:
    	// -rw-r--r--false
    	// -rwxrwxrwxtrue
    }

路径操作

Join


Chmod

  • 说明:使用指定的权限,更改指定路径的文件权限。

    说明:将多个字符串路径通过`/`进行连接。

  • 示例:

    Code Block
    languagego
    func ExampleJoinExampleChmod() {
    	// init
    	var (
    		dirPathpath  = gfile.Join(gfile.TempDir("gfile_example_basic_dir"), "file1")
    	)
    
    	// Get a FileInfo describing the named file.
    	stat, err := gfile.Stat(path)
    		filePath = "file1"
    	if err != nil {
    		fmt.Println(err.Error())
    	}
    	// Show original mode
    	fmt.Println(stat.Mode())
    
    	// Joins string array paths withChange file separator of current system.model
    	joinString := gfile.JoinChmod(dirPathpath, filePath)
    
    	fmt.Println(joinStringgfile.DefaultPermCopy)
    
    	// Output:
    	// /tmp/gfile_example_basic_dir/file1
    }

Exists

  • 说明:检查给定的路径是否存在 。
  • 示例:

    Code Block
    languagego
    func ExampleExists() {
    	// init
    	var (
    		path Get a FileInfo describing the named file.
    	stat, _ = gfile.Join(gfile.TempDir("gfile_example_basic_dir"), "file1")
    	)Stat(path)
    	// ChecksShow whetherthe given `path` exist.modified mode
    	fmt.Println(gfilestat.ExistsMode(path))
    
    	// Output:
    	// -rw-r--r--
    	// true-rwxrwxrwx
    }

IsDir


路径操作

Join

  • 说明:将多个字符串路径通过`/`进行连接。说明:检查给定的路径是否是文件夹。
  • 示例:

    Code Block
    languagego
    func ExampleIsDirExampleJoin() {
    	// init
    	var (
    		path  dirPath   = gfile.TempDir("gfile_example_basic_dir")
    		filePath = gfile.Join(gfile.TempDir("gfile_example_basic_dir"), "file1")
    	)
    	// Checks whether given `path` a directory.
    	fmt.Println(gfile.IsDir(path))
    	// Joins string array paths with file separator of current system.
    	joinString := gfile.Join(dirPath, filePath)
    
    	fmt.Println(gfile.IsDir(filePathjoinString))
    
    	// Output:
    	// true
    	// false/tmp/gfile_example_basic_dir/file1
    }
Pwd

Exists

  • 说明:获取当前工作路径。说明:检查给定的路径是否存在 。
  • 示例:

    Code Block
    languagego
    func ExamplePwdExampleExists() {
    	// Get absolute path of current working directory init
    	var (
    		path = gfile.Join(gfile.TempDir("gfile_example_basic_dir"), "file1")
    	)
    	// Checks whether given `path` exist.
    	fmt.Println(gfile.PwdExists(path))
    
    	// May Output:
    	// xxx/gf/os/gfiletrue
    }
Chdir

IsDir

  • 说明:使用给定的路径,更改当前的工作路径。说明:检查给定的路径是否是文件夹。
  • 示例:

    Code Block
    languagego
    func ExampleChdirExampleIsDir() {
    	// init
    	var (
    		path     = gfile.TempDir("gfile_example_basic_dir")
    		filePath = gfile.Join(gfile.TempDir("gfile_example_basic_dir"), "file1")
    	)
    	// Checks whether Getgiven current`path` workinga directory.
    	fmt.Println(gfile.PwdIsDir())
    
    	// Changes the current working directory to the named directory.
    	gfile.Chdir(path)
    
    	// Get current working directorypath))
    	fmt.Println(gfile.PwdIsDir(filePath))
    
    	// May Output:
    	// xxx/gf/os/gfiletrue
    	// /tmp/gfile_example_basic_dir/file1false
    }
IsFile

Chdir

  • 说明:检查给定的路径是否是文件。说明:使用给定的路径,更改当前的工作路径。
  • 示例:

    Code Block
    languagego
    func ExampleIsFileExampleChdir() {
    	// init
    	var (
    		filePathpath = gfile.Join(gfile.TempDir("gfile_example_basic_dir"), "file1")
    	)
    	dirPath// Get = current working directory
    	fmt.Println(gfile.TempDir("gfile_example_basic_dir"Pwd())
    	)
    	// ChecksChanges whetherthe givencurrent `path`working adirectory file,to whichthe means it's not a named directory.
    	fmt.Println(gfile.IsFile(filePath))Chdir(path)
    
    	// Get current working directory
    	fmt.Println(gfile.IsFilePwd(dirPath))
    
    	// May Output:
    	// truexxx/gf/os/gfile
    	// false/tmp/gfile_example_basic_dir/file1
    }
DirNames

IsFile

  • 说明:获取给定路径下的文件列表,返回的是一个切片。说明:检查给定的路径是否是文件。
  • 示例:

    Code Block
    languagego
    func ExampleDirNamesExampleIsFile() {
    	// init
    	var (
    		filePath = gfile.Join(gfile.TempDir("gfile_example_basic_dir"), "file1")
    		pathdirPath  = gfile.TempDir("gfile_example_basic_dir")
    	)
    	// Get sub-file names of given directory `path`.
    	dirNames, _ := gfile.DirNames(path)
    
    	fmt.Println(dirNames Checks whether given `path` a file, which means it's not a directory.
    	fmt.Println(gfile.IsFile(filePath))
    	fmt.Println(gfile.IsFile(dirPath))
    
    	// May Output:
    	// true
    	// [file1]false
    }
Glob

DirNames

  • 说明:模糊搜索给定路径下的文件列表,支持正则,第二个参数控制返回的结果是否带上绝对路径。说明:获取给定路径下的文件列表,返回的是一个切片。

  • 示例:

    Code Block
    languagego
    func ExampleGlobExampleDirNames() {
    	// init
    	var (
    		path = gfile.Pwd() + gfile.Separator + "*TempDir("gfile_example_basic_test.godir")
    	)
    	// 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 names of given directory `path`.
    	dirNames, _ := gfile.GlobDirNames(path, false)
    
    	fmt.Println(matchNamesdirNames)
    
    	// May Output:
    	// [gfile_z_example_basic_test.go]
    	// [xxx/gf/os/gfile/gfile_z_example_basic_test.gofile1]
    }
IsReadable

Glob

  • 说明:检查给定的路径是否可读。说明:模糊搜索给定路径下的文件列表,支持正则,第二个参数控制返回的结果是否带上绝对路径。

  • 示例:

    Code Block
    languagego
    func ExampleIsReadableExampleGlob() {
    	// init
    	var (
    		path = gfile.Pwd() + gfile.Separator + "testdata/readline/file.log*_example_basic_test.go"
    	)
    
    	// ChecksGet whether given `path` is readable.
    	fmt.Println(gfile.IsReadable(path))
    
    	// Output:sub-file names of given directory `path`.
    	// true
    }

Abs

  • 说明:返回路径的绝对路径。

  • 示例:

    Code Block
    languagego
    func ExampleAbs() {
    	// init
    	var (
    		path Only show file name
    	matchNames, _ := gfile.Join(gfile.TempDir("gfile_example_basic_dir"), "file1")
    	Glob(path, true)
    
    	fmt.Println(matchNames)
    
    	// Get an absolute representation of path. Show full path of the file
    	matchNames, _ = gfile.Glob(path, false)
    
    	fmt.Println(gfile.Abs(path)(matchNames)
    
    	// May Output:
    	// [gfile_z_example_basic_test.go]
    	// /tmp[xxx/gf/os/gfile/gfile_z_example_basic_dir/file1test.go]
    }
RealPath

IsReadable

  • 说明:获取给定路径的绝对路径地址,如果文件不存在则返回空。说明:检查给定的路径是否可读。

  • 示例:

    Code Block
    languagego
    func ExampleRealPathExampleIsReadable() {
    	// init
    	var (
    		realPathpath  = gfile.Join(gfile.TempDir("gfile_example_basic_dir"), "file1")
    		worryPath = gfile.Join(gfile.TempDir("gfile_example_basic_dir"), "worryFile")Pwd() + gfile.Separator + "testdata/readline/file.log"
    	)
    
    	// fetchChecks anwhether absolutegiven representation`path` ofis pathreadable.
    	fmt.Println(gfile.RealPath(realPath))
    	fmt.Println(gfile.RealPath(worryPathIsReadable(path))
    
    	// Output:
    	// /tmp/gfile_example_basic_dir/file1
    	//
    true
    }
SelfPath

Abs

  • 说明:获取当前运行程序的绝对路径。说明:返回路径的绝对路径。

  • 示例:

    Code Block
    languagego
    func ExampleSelfPathExampleAbs() {
    	// init
    	var (
    		path = gfile.Join(gfile.TempDir("gfile_example_basic_dir"), "file1")
    	)
    
    	// Get an absolute filerepresentation path of current running processpath.
    	fmt.Println(gfile.SelfPathAbs(path))
    
    	// May Output:
    	// xxx/tmp/gfile_example__github_com_gogf_gf_v2_os_gfile__ExampleSelfPathbasic_dir/file1
    }
SelfName

RealPath

  • 说明:获取当前运行程序的名称。说明:获取给定路径的绝对路径地址,如果文件不存在则返回空。

  • 示例:

    Code Block
    languagego
    func ExampleSelfNameExampleRealPath() {
    	// init
    	var (
    		realPath  = gfile.Join(gfile.TempDir("gfile_example_basic_dir"), "file1")
    		worryPath = gfile.Join(gfile.TempDir("gfile_example_basic_dir"), "worryFile")
    	)
    
    	// fetch Getan fileabsolute namerepresentation of current running processpath.
    	fmt.Println(gfile.SelfNameRealPath(realPath))
    	fmt.Println(gfile.RealPath(worryPath))
    
    	// May Output:
    	// /tmp/gfile_example__github_com_gogf_gf_v2_os_gfile__ExampleSelfNamebasic_dir/file1
    	//
    }
Basename

SelfName

  • 说明:获取给定路径中的最后一个元素,包含扩展名。说明:获取当前运行程序的名称。

  • 示例:

    Code Block
    languagego
    func ExampleBasenameExampleSelfName() {
    	// init
    	var (
    		path = gfile.Pwd() + gfile.Separator + "testdata/readline/file.log"
    	)
    
    	// Get thefile lastname element of path,current which contains file extension.running process
    	fmt.Println(gfile.BasenameSelfName(path))
    
    	// May Output:
    	// file.log___github_com_gogf_gf_v2_os_gfile__ExampleSelfName
    }
Name

Basename

  • 说明:获取给定路径中的最后一个元素,不包含扩展名。说明:获取给定路径中的最后一个元素,包含扩展名。

  • 示例:

    Code Block
    languagego
    func ExampleNameExampleBasename() {
    	// init
    	var (
    		path = gfile.Pwd() + gfile.Separator + "testdata/readline/file.log"
    	)
    
    	// Get the last element of path, which withoutcontains file extension.
    	fmt.Println(gfile.NameBasename(path))
    
    	// Output:
    	// file.log
    }
Dir

Name

  • 说明:获取给定路径的目录部分,排除最后的元素。说明:获取给定路径中的最后一个元素,不包含扩展名。

  • 示例:

    Code Block
    languagego
    func ExampleDirExampleName() {
    	// init
    	var (
    		path = gfile.JoinPwd(gfile.TempDir("gfile_example_basic_dir"), "file1")) + gfile.Separator + "testdata/readline/file.log"
    	)
    
    	// Get all but the last element of path, typically the path's directorywithout file extension.
    	fmt.Println(gfile.DirName(path))
    
    	// Output:
    	// /tmp/gfile_example_basic_dirfile
    }
Ext

Dir

  • 说明:获取给定路径的扩展名,包含`.`。说明:获取给定路径的目录部分,排除最后的元素。

  • 示例:

    Code Block
    languagego
    func ExampleExtExampleDir() {
    	// init
    	var (
    		path = gfile.PwdJoin() + gfile.Separator + "testdata/readline/file.log"gfile.TempDir("gfile_example_basic_dir"), "file1")
    	)
    
    	// Get all but the filelast element nameof extensionpath, usedtypically bythe path's directory.
    	fmt.Println(gfile.ExtDir(path))
    
    	// Output:
    	// .log/tmp/gfile_example_basic_dir
    }
ExtName

Ext

  • 说明:获取给定路径的扩展名,不包含`说明:获取给定路径的扩展名,包含`.`。

  • 示例:

    Code Block
    languagego
    func ExampleExtNameExampleExt() {
    	// init
    	var (
    		path = gfile.Pwd() + gfile.Separator + "testdata/readline/file.log"
    	)
    
    	// Get the file name extension used by path but the result does not contains symbol '.' extension used by path.
    	fmt.Println(gfile.ExtNameExt(path))
    
    	// Output:
    	// .log
    }
TempDir

ExtName

  • 说明:获取拼接系统临时路径后的绝对地址。说明:获取给定路径的扩展名,不包含`.`。

  • 示例:

    Code Block
    languagego
    func ExampleTempDirExampleExtName() {
    	// init
    	var (
    		fileNamepath = "gfile_example_basic_dirgfile.Pwd() + gfile.Separator + "testdata/readline/file.log"
    	)
    
    	// Get the fetchfile anname absoluteextension representationused of path.
    	path := gfile.TempDir(fileName)
    
    by path but the result does not contains symbol '.'.
    	fmt.Println(gfile.ExtName(path))
    
    	// Output:
    	// /tmp/gfile_example_basic_dirlog
    }








Panel
titleContent Menu

Table of Contents