×

Golang Tutorial

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

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 [Last updated : March 05, 2023]

Joining multiple paths into a single path in Golang

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?

Golang code 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 »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.