filepath.Clean() Function in Golang With Examples

In Go language, path package used for paths separated by forwarding slashes, such as the paths in URLs. The filepath.Clean() function in Go language used to return the shortest path name equivalent to the specified path by purely lexical processing. Moreover, this function is defined under the path package. Here, you need to import the “path/filepath” package in order to use these functions.

This function applies the Below rules iteratively until no further processing can be done:

  • It Replaces multiple Separator elements with a single one.
  • If the specified path is an empty string, it returns the string “.”.
  • It eliminates each . path name element (the current directory).
  • It eliminates each inner .. path name element (the parent directory) along with the non-.. element that precedes it.
  • It eliminates .. elements that begin a rooted path: that is, replace “/..” by “/” at the beginning of a path, assuming Separator is ‘/’.

Syntax:

func Clean(path string) string

Here, ‘path’ is the specified path.

Return Value: It returns the shortest path name equivalent to the specified path by purely lexical processing.



Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Golang program to illustrate the usage of
// filepath.Clean() function
  
// Including the main package
package main
  
// Importing fmt and path/filepath
import (
    "fmt"
    "path/filepath"
)
  
// Calling main
func main() {
  
    // Calling the Clean() function
    fmt.Println(filepath.Clean("/GFG/./../Geeks"))
    fmt.Println(filepath.Clean("GFG/../Geeks"))
    fmt.Println(filepath.Clean("..GFG/./../Geeks"))
    fmt.Println(filepath.Clean("gfg/../../../Geek/GFG"))
}

chevron_right


Output:

/Geeks
Geeks
Geeks
../../Geek/GFG

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Golang program to illustrate the usage of
// filepath.Clean() function
  
// Including the main package
package main
  
// Importing fmt and path/filepath
import (
    "fmt"
    "path/filepath"
)
  
// Calling main
func main() {
  
    // Calling the Clean() function
    fmt.Println(filepath.Clean(""))
    fmt.Println(filepath.Clean("."))
    fmt.Println(filepath.Clean("///"))
    fmt.Println(filepath.Clean("/.//"))
    fmt.Println(filepath.Clean("/./"))
    fmt.Println(filepath.Clean(":/"))
}

chevron_right


Output:

.
.
/
/
/
:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.