I Spy Code - GO

Join path elements into a single path

Question:

How do you join multiple paths into a single path in go lang?

Answer:

The function path.Join() joins any number of path elements into a single path, adding a separating slash if necessary.

Here is a go lang example that

Source: (example.go)

package main
 
import (
   "fmt"
   "path"
)
 
func main() {
   fmt.Println(path.Join("a", "b", "c"))
   fmt.Println(path.Join("a", "b/c"))
   fmt.Println(path.Join("a/b", "c"))
   fmt.Println(path.Join("a/b", "/c"))
}
 

Output:

$ go run example.go
a/b/c
a/b/c
a/b/c
a/b/c

References:

https://golang.org/pkg/path/#Join