Q:

How to join multiple paths into a single path in Golang?

belongs to collection: Golang path/filepath Package Programs

0

In the Go programming language, to join the given multiple paths into a single path – we use the Join() function of path/filepath package. The Join() function joins any number of the given path elements into a single path, separating them with an OS-specific Separator.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Syntax:

func Join(elem ...string) string

Consider the below Golang program demonstrating how to join multiple paths into a single path?

package main

import (
	"fmt"
	"path/filepath"
)

func main() {
	// Defining path elements
	path1 := "C:"
	path2 := "/programs/course1/"
	path3 := "hello1.go"

	// Calling Join() function to join path elemenets
	// (path1, path2, path3)
	result := filepath.Join(path1, path2, path3)

	// Printing the result
	fmt.Println("The final path is: ", result)
}

Output

The final path is:  C:/programs/course1/hello1.go

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

How to split a path into the directory and file na... >>
<< How to check if a path is an absolute path in Gola...