filepath.Clean() Function in Golang With Examples Last Updated : 10 May, 2020 Comments Improve Suggest changes Like Article Like Report 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: C // 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")) } Output: /Geeks Geeks Geeks ../../Geek/GFG Example 2: C // 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(":/")) } Output: . . / / / : Comment More infoAdvertise with us Next Article filepath.Clean() Function in Golang With Examples K Kanchan_Ray Follow Improve Article Tags : Go Language Golang-filepath Similar Reads filepath.Abs() 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.Abs() function in Go language used to return an absolute representation of the specified path. If the path is not absolute it will be joined with the current working directory to turn 2 min read filepath.Base() 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.Base() function in Go language used to return the last element of the specified path. Here the trailing path separators are removed before extracting the last element. If the path is 2 min read filepath.Dir() 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.Dir() function in Go language used to return all the elements of the specified path except the last element. After dropping the final element, Dir calls Clean on the path and trailing 2 min read filepath.Ext() 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.Ext() function in Go language used to return the file name extension used by the specified path. The extension is the suffix beginning at the final dot in the final element of path; i 2 min read filepath.Join() 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.Join() function in Go language used to join any number of the specified path elements into a single path, adding a Separator if necessary. This function calls Clean on the result and 2 min read Like