Go filepath
最后修改时间 2024 年 4 月 11 日
在本文中,我们将展示如何在 Golang 中处理文件名路径。这些实用程序位于 path/filepath 包中。
path/filepath
包试图与目标操作系统定义的文件路径兼容。
Go filepath.Abs, filepath.IsAbs
绝对路径是来自根目录的完整路径。相对路径被定义为与当前工作目录相关的路径。
filepath.Abs
返回路径的绝对表示。filepath.IsAbs
检查给定路径是否为绝对路径。
package main import ( "fmt" "log" "path/filepath" ) func main() { fname := "./main.go" abs_fname, err := filepath.Abs(fname) if err != nil { log.Fatal(err) } if filepath.IsAbs(fname) { fmt.Printf("%s - is an absolute path\n", fname) } else { fmt.Printf("%s - is not an absolute path\n", fname) } if filepath.IsAbs(abs_fname) { fmt.Printf("%s - is an absolute path\n", abs_fname) } else { fmt.Printf("%s - is not an absolute path\n", abs_fname) } }
示例同时使用了 filepath.Abs
和 filepath.IsAbs
函数。
$ go run main.go ./main.go - is not an absolute path /home/jano/Documents/prog/go/filepath/abs/main.go - is an absolute path
Go filepath.Base, filepath.Dir
filepath.Base
函数返回路径的最后一个元素,filepath.Dir
返回路径中除最后一个元素外的所有部分,通常是文件目录。
package main import ( "fmt" "log" "path/filepath" ) func main() { p, err := filepath.Abs("./main.go") if err != nil { log.Fatal(err) } fmt.Println(p) fmt.Printf("Base: %s\n", filepath.Base(p)) fmt.Printf("Dir: %s\n", filepath.Dir(p)) }
示例打印出 main.go
文件的文件名和目录名。
$ go run main.go /home/jano/Documents/prog/go/filepath/base-dir/main.go Base: main.go Dir: /home/jano/Documents/prog/go/filepath/base-dir
Go filepath.Ext
filepath.Ext
返回路径使用的文件名扩展名。
package main import ( "fmt" "path/filepath" ) func main() { p := "/home/user7/media/aliens.mp4" ext := filepath.Ext(p) fmt.Println("File extension:", ext) p = "./main.go" ext = filepath.Ext(p) fmt.Println("File extension:", ext) }
示例返回两个路径的文件扩展名。
$ go run main.go File extension: .mp4 File extension: .go
Go filepath.Clean
filepath.Clean
函数会清理文件路径中的重复项和不规范之处。
package main import ( "fmt" "path" ) func main() { paths := []string{ "home/user7", "home//user7", "home/user7/.", "home/user7/Documents/..", "/../home/user7", "/../home/Documents/../././/user7", "", } for _, p := range paths { fmt.Printf("%q = %q\n", p, path.Clean(p)) } }
在示例中,我们清理了几个文件路径。
$ go run main.go "home/user7" = "home/user7" "home//user7" = "home/user7" "home/user7/." = "home/user7" "home/user7/Documents/.." = "home/user7" "/../home/user7" = "/home/user7" "/../home/Documents/../././/user7" = "/home/user7" "" = "."
Go filepath.Split, filepath.SplitList
filepath.Split
函数将给定路径分割成目录和文件名组件。filepath.SplitList
函数分割由操作系统特定行分隔符连接的路径列表。
package main import ( "fmt" "log" "os" "path/filepath" ) func main() { cwd, err := os.Getwd() if err != nil { log.Fatal(err) } dir, file := filepath.Split(cwd) fmt.Printf("Directory: %s\n", dir) fmt.Printf("File: %s\n", file) home, err := os.UserHomeDir() if err != nil { log.Fatal(err) } fmt.Println("-------------------------") dir, file = filepath.Split(home) fmt.Printf("Directory: %s\n", dir) fmt.Printf("File: %s\n", file) path_env := os.Getenv("PATH") paths := filepath.SplitList(path_env) for _, p := range paths { fmt.Println(p) } }
在示例中,我们首先分割当前工作目录和用户主目录。然后我们分割 PATH 变量中的路径列表。
Go filepath.Walk
filepath.Walk
函数会遍历以 root 为根的文件树。
func Walk(root string, fn WalkFunc) error
它会为树中的每个文件或目录(包括 root)调用 WalkFunc。
package main import ( "fmt" "log" "os" "path/filepath" ) func main() { var files []string root := "/home/jano/Documents" err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error { if err != nil { fmt.Println(err) return nil } if !info.IsDir() && filepath.Ext(path) == ".txt" { files = append(files, path) } return nil }) if err != nil { log.Fatal(err) } for _, file := range files { fmt.Println(file) } }
在示例中,我们遍历 Documents 目录中的文件,并列出所有文本文件(扩展名为 .txt 的文件)。
Go filepath.Glob
filepath.Glob
返回所有匹配模式的文件名,如果没有匹配的文件则返回 nil
。
func Glob(pattern string) (matches []string, err error)
这是 filepath.Glob
函数的语法。
package main import ( "fmt" "log" "path/filepath" ) func main() { files, err := filepath.Glob("/home/jano/Documents/prog/go/**/**/*.go") fmt.Println(len(files)) if err != nil { log.Fatal(err) } for _, file := range files { fmt.Println(file) } }
示例列出给定目录中的所有 Go 文件。使用 **
模式,列出是递归的。
Go filepath.VolumeName
filepath.VolumeName
函数在 Windows 上返回前导卷名。在其他平台上,它返回一个空字符串。
package main import ( "fmt" "log" "path/filepath" ) func main() { fname := "./main.go" ap, err := filepath.Abs(fname) if err != nil { log.Fatal(err) } fmt.Println(filepath.VolumeName(ap)) }
示例打印 main.go 文件的卷名。
$ go run main.go C:
来源
在本文中,我们使用 filename/path
包在 Go 中处理了文件名路径。