Skip to content
Related Articles

Related Articles

filepath Package in Golang
  • Last Updated : 22 Jun, 2020
GeeksforGeeks - Summer Carnival Banner

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.

FunctionDescription
AbsThis function is used to return an absolute representation of path.
BaseThis function is used to return the last element of path.
CleanThis function is used to return the shortest path name equivalent to the path by purely lexical processing.
DirThis function is used to return all but the last element of path, typically the path’s directory.
EvalSymlinksThis function is used to return the path name after the evaluation of any symbolic links.
ExtThis function is used to return the file name extension used by path.
FromSlashThis function is used to return the result of replacing each slash (‘/’) character in path with a separator character.
GlobThis function is used to return the names of all files matching pattern or nil if there is no matching file.
HasPrefixIt exists for historical compatibility and should not be used.
IsAbsThis function is used to check whether the path is absolute.
JoinThis function is used to join any number of path elements into a single path, separating them with an OS specific Separator.
MatchThis function is used to check whether name matches the shell file name pattern or not.
RelThis function is used to return a relative path that is lexically equivalent to targpath when joined to basepath with an intervening separator.
SplitThis function is used to split path immediately following the final Separator, separating it into a directory and file name component.
SplitListThis function is used to splits a list of paths joined by the OS-specific ListSeparator, usually found in PATH or GOPATH environment variables.
ToSlashThis function is used to return the result of replacing each separator character in path with a slash (‘/’) character.
VolumeNameThis function is used to returns leading volume name.
WalkThis 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 :




// 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")) 

Output:

G/F/G
G/F/G
gfg/GFG
Geeks/for/Geeks

Example 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")) 

Output:

/home/gfg 
/.gfg 
/gfg 
/:gfg 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :