Categories: GoFrame 教程

GoFrame gfile-路径操作

路径操作

Join

  • 说明:将多个字符串路径通过`/`进行连接。
  • 格式:
func Join(paths ...string) string 
  • 示例:
func ExampleJoin() {
 // init
 var (
  dirPath  = gfile.TempDir("gfile_example_basic_dir")
  filePath = "file1"
 )

 // Joins string array paths with file separator of current system.
 joinString := gfile.Join(dirPath, filePath)

 fmt.Println(joinString)

 // Output:
 // /tmp/gfile_example_basic_dir/file1
}

Abs

  • 说明:返回路径的绝对路径。
  • 格式:
func Abs(path string) string
  • 示例:
func ExampleAbs() {
 // init
 var (
  path = gfile.Join(gfile.TempDir("gfile_example_basic_dir"), "file1")
 )

 // Get an absolute representation of path.
 fmt.Println(gfile.Abs(path))

 // Output:
 // /tmp/gfile_example_basic_dir/file1
}

RealPath

  • 说明:获取给定路径的绝对路径地址。
  • 注意:如果文件不存在则返回空。
  • 格式:
func RealPath(path string) string
  • 示例:
func ExampleRealPath() {
 // init
 var (
  realPath  = gfile.Join(gfile.TempDir("gfile_example_basic_dir"), "file1")
  worryPath = gfile.Join(gfile.TempDir("gfile_example_basic_dir"), "worryFile")
 )

 // fetch an absolute representation of path.
 fmt.Println(gfile.RealPath(realPath))
 fmt.Println(gfile.RealPath(worryPath))

 // Output:
 // /tmp/gfile_example_basic_dir/file1
 //
}

SelfName

  • 说明:获取当前运行程序的名称。
  • 格式:
func SelfName() string 
  • 示例:
func ExampleSelfName() {

 // Get file name of current running process
 fmt.Println(gfile.SelfName())

 // May Output:
 // ___github_com_gogf_gf_v2_os_gfile__ExampleSelfName
}

Basename

  • 说明:获取给定路径中的最后一个元素,包含扩展名。
  • 格式:
func Basename(path string) string
  • 示例:
func ExampleBasename() {
 // init
 var (
  path = gfile.Pwd() + gfile.Separator + "testdata/readline/file.log"
 )

 // Get the last element of path, which contains file extension.
 fmt.Println(gfile.Basename(path))

 // Output:
 // file.log
}

Name

  • 说明:获取给定路径中的最后一个元素,不包含扩展名。
  • 格式:
func Name(path string) string
  • 示例:
func ExampleName() {
 // init
 var (
  path = gfile.Pwd() + gfile.Separator + "testdata/readline/file.log"
 )

 // Get the last element of path without file extension.
 fmt.Println(gfile.Name(path))

 // Output:
 // file
}

Dir

  • 说明:获取给定路径的目录部分,排除最后的元素。
  • 格式:
func Dir(path string) string 
  • 示例:
func ExampleDir() {
 // init
 var (
  path = gfile.Join(gfile.TempDir("gfile_example_basic_dir"), "file1")
 )

 // Get all but the last element of path, typically the paths directory.
 fmt.Println(gfile.Dir(path))

 // Output:
 // /tmp/gfile_example_basic_dir
}

Ext

  • 说明:获取给定路径的扩展名,包含`.`。
  • 格式:
func Ext(path string) string
  • 示例:
func ExampleExt() {
 // init
 var (
  path = gfile.Pwd() + gfile.Separator + "testdata/readline/file.log"
 )

 // Get the file name extension used by path.
 fmt.Println(gfile.Ext(path))

 // Output:
 // .log
}

ExtName

  • 说明:获取给定路径的扩展名,不包含`.`。
  • 格式:
func ExtName(path string) string
  • 示例:
func ExampleExtName() {
 // 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 ..
 fmt.Println(gfile.ExtName(path))

 // Output:
 // log
}

MainPkgPath

  • 说明:获取main文件(主入口)所在的绝对路径,。
  • 注意:
    • 该方法仅在开发环境中可用,同时仅在源代码开发环境中有效,build二进制后将显示源代码的路径地址。
    • 第一次调用该方法时,如果处于异步的goroutine中,可能会无法获取主包的路径
  • 格式:
func MainPkgPath() string
  • 示例:
func Test() {
 fmt.Println("main pkg path on main :", gfile.MainPkgPath())
 char := make(chan int, 1)
 go func() {
  fmt.Println("main pkg path on goroutine :", gfile.MainPkgPath())
  char <- 1
 }()
 select {
 case <-char:
 }
 // Output:
 // /xxx/xx/xxx/xx
 // /xxx/xx/xxx/xx
}
// 二进制包
$ ./testDemo 
main pkg path on main : /xxx/xx/xxx/xx
main pkg path on goroutine : /xxx/xx/xxx/xx

terry

这个人很懒,什么都没有留下~

Share
Published by
terry

Recent Posts

聊聊vue3中的defineProps

在Vue 3中,defineP…

1 周 ago

在 Chrome 中删除、允许和管理 Cookie

您可以选择删除现有 Cooki…

2 周 ago

自定义指令:聊聊vue中的自定义指令应用法则

今天我们来聊聊vue中的自定义…

3 周 ago

聊聊Vue中@click.stop和@click.prevent

一起来学下聊聊Vue中@cli…

4 周 ago

Nginx 基本操作:启动、停止、重启命令。

我们来学习Nginx基础操作:…

1 月 ago

Vue3:手动清理keep-alive组件缓存的方法

Vue3中手动清理keep-a…

1 月 ago