×

Go Tutorial

Go Basics

Go Variables

Go Literals

Go Type Handling

Go Operators

Go Decision Making

Go Loops

Go Functions

Go String

Go Arrays

Go Slices

Go Maps

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 »



Advertisement
Advertisement


Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

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