Go文件路径教程展示了如何在Golang中使用文件名路径。实用程序位于路径/文件路径包中。
path/filepath
包试图与目标操作系统定义的文件路径兼容。
$ go version go version go1.18.1 linux/amd64
我们使用Go版本1.18。
转到文件路径.Abs,文件路径.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
转到文件路径.Base、文件路径.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
file.
$ 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
转到文件路径.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
Gofilepath.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" "" = "."
去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变量中拆分路径列表。
前往文件路径.Walk
filepath.Walk
函数遍历以根为根的文件树。
func Walk(root string, fn WalkFunc) error
它为树中的每个文件或目录调用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的文件)。
转到文件路径.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文件。使用**
模式,列表是递归的。
转到文件路径.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中处理文件名路径。
列出所有Go教程。