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 empty, Base returns “.”. If the path consists entirely of separators, Base returns a single separator. Moreover, this function is defined under the path package. Here, you need to import the “path/filepath” package in order to use these functions.
Syntax:
func Base(path string) string
Here, ‘path’ is the specified path.
Return Value: It return the last element of the specified path. If the path is empty, this function returns “.”. If the path consists entirely of separators, this function returns a single separator.
Example 1:
package main
import (
"fmt"
"path/filepath"
)
func main() {
fmt.Println(filepath.Base( "/home/gfg" ))
fmt.Println(filepath.Base( ".gfg" ))
fmt.Println(filepath.Base( "/gfg" ))
fmt.Println(filepath.Base( ":gfg/GFG" ))
}
|
Output:
gfg
.gfg
gfg
GFG
Example 2:
package main
import (
"fmt"
"path/filepath"
)
func main() {
fmt.Println(filepath.Base( "" ))
fmt.Println(filepath.Base( "." ))
fmt.Println(filepath.Base( "/" ))
fmt.Println(filepath.Base( "/." ))
fmt.Println(filepath.Base( "//" ))
fmt.Println(filepath.Base( ":/" ))
}
|
Output:
.
.
/
.
/
:
Last Updated :
10 May, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...