Open In App

filepath Package in Golang

Improve
Improve
Like Article
Like
Save
Share
Report

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 :




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


Last Updated : 22 Jun, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads