filepath Package in Golang
Last Updated :
22 Jun, 2020
Go language provides inbuilt support for implementing utility routines for manipulating filename paths in a way compatible with the target operating system-defined file paths with the help of the filepath package. This package uses either forward slashes or backslashes (depending on the operating system) to process paths e.g., URLs that always use forward slashes.
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. |
Example 1 :
package main
import (
"fmt"
"path/filepath"
)
func main() {
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" ))
}
|
Output:
G/F/G
G/F/G
gfg/GFG
Geeks/for/Geeks
Example 2:
package main
import (
"fmt"
"path/filepath"
)
func main() {
fmt.Println(filepath.Abs( "/home/gfg" ))
fmt.Println(filepath.Abs( ".gfg" ))
fmt.Println(filepath.Abs( "/gfg" ))
fmt.Println(filepath.Abs( ":gfg" ))
}
|
Output:
/home/gfg
/.gfg
/gfg
/:gfg
Share your thoughts in the comments
Please Login to comment...