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 all empty strings are ignored. 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 Join(elem ...string) string
Here, ‘elem’ is the specified path elements.
Return Value: It returns the joined single path.
Example 1:
package main
import (
"fmt"
"path/filepath"
)
func main() {
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:
package main
import (
"fmt"
"path/filepath"
)
func main() {
fmt.Println(filepath.Join( "/" , "/" ))
fmt.Println(filepath.Join( "" ))
fmt.Println(filepath.Join( "a/b" , "/c" ))
fmt.Println(filepath.Join( "5" , "10" ))
fmt.Println(filepath.Join( "." ))
}
|
Output:
/
a/b/c
5/10
.
Last Updated :
10 May, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...