Open In App

filepath.Join() Function in Golang With Examples

Improve
Improve
Like Article
Like
Save
Share
Report

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:




// 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.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("/", "/"))
    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
Previous
Next
Share your thoughts in the comments
Similar Reads