Standard library / path

path.Join()

Joins nonempty elements using slashes and cleans the result; not a URL parser or a security boundary. Example uses a disposable temporary workspace. Expected stdout: 'assets/logo.svg\n'.

Formal Syntax & Structure

go syntaxgo
func Join(elem ...string) string

Parameters & Attributes

NameTypeDescription
elem...stringPath elements in order.

Return Value

string

Joins nonempty elements using slashes and cleans the result; not a URL parser or a security boundary.

Working Example

Example Snippetgo
package main
import (  "fmt"  "os"  "path")
func check(e error) {  if e != nil {    panic(e)  }}func workspace() func() {  root, e := os.MkdirTemp("", "bytemind-go-")  check(e)  old, e := os.Getwd()  check(e)  check(os.Chdir(root))  return func() { check(os.Chdir(old)); check(os.RemoveAll(root)) }}func main() { defer workspace()(); fmt.Println(path.Join("docs", "../assets", "logo.svg")) }
Go Reference