Go 语言为实现实用程序提供了内置支持,以在 filepath 包的帮助下以与目标操作系统定义的文件路径兼容的方式操作文件名路径。此包使用正斜杠或反斜杠(取决于操作系统)来处理路径,例如始终使用正斜杠的 URL。
Function | Description |
---|---|
Abs | This function is used to return an absolute representation of path. |
Base | This function is used to return the last element of path. |
Clean | This function is used to return the shortest path name equivalent to the path by purely lexical processing. |
Dir | This function is used to return all but the last element of path, typically the path’s directory. |
EvalSymlinks | This function is used to return the path name after the evaluation of any symbolic links. |
Ext | This function is used to return the file name extension used by path. |
FromSlash | This function is used to return the result of replacing each slash (‘/’) character in path with a separator character. |
Glob | This function is used to return the names of all files matching pattern or nil if there is no matching file. |
HasPrefix | It exists for historical compatibility and should not be used. |
IsAbs | This function is used to check whether the path is absolute. |
Join | This function is used to join any number of path elements into a single path, separating them with an OS specific Separator. |
Match | This function is used to check whether name matches the shell file name pattern or not. |
Rel | This function is used to return a relative path that is lexically equivalent to targpath when joined to basepath with an intervening separator. |
Split | This function is used to split path immediately following the final Separator, separating it into a directory and file name component. |
SplitList | This function is used to splits a list of paths joined by the OS-specific ListSeparator, usually found in PATH or GOPATH environment variables. |
ToSlash | This function is used to return the result of replacing each separator character in path with a slash (‘/’) character. |
VolumeName | This function is used to returns leading volume name. |
Walk | This function is used to walk the file tree rooted at root, calling walkFn for each file or directory in the tree, including root. |
示例 1:
// Golang program to illustrate the usage of
// filepath.Join() function
// Including the main package
package main
// Importing fmt and path/filepath
import (
"fmt"
"path/filepath"
)
// Calling main
func main() {
// Calling the Join() function
fmt.Println(filepath.Join("G", "F", "G"))
fmt.Println(filepath.Join("G/F", "G"))
fmt.Println(filepath.Join("gfg", "GFG"))
fmt.Println(filepath.Join("Geeks", "for", "Geeks"))
}
输出:
G/F/G
G/F/G
gfg/GFG
Geeks/for/Geeks
示例 2:
// Golang program to illustrate the usage of
// filepath.Abs() function
// Including the main package
package main
// Importing fmt and path/filepath
import (
"fmt"
"path/filepath"
)
// Calling main
func main() {
// Calling the Abs() function to get
// absolute representation of the specified path
fmt.Println(filepath.Abs("/home/gfg"))
fmt.Println(filepath.Abs(".gfg"))
fmt.Println(filepath.Abs("/gfg"))
fmt.Println(filepath.Abs(":gfg"))
}
输出:
/home/gfg
/.gfg
/gfg
/:gfg