ADVERTISEMENT
ADVERTISEMENT

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

Given a path, we have to join multiple paths into a single path in Golang.
Submitted by IncludeHelp, on October 28, 2021

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.

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

Golang path/filepath Package Programs »



ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.