Standard library / path/filepath
path/filepath.Join()
Combines nonempty path elements and cleans the result using OS separators. Joining an untrusted path alone does not establish containment. Example uses a disposable temporary workspace. Expected stdout: 'b\n'.
Formal Syntax & Structure
go syntaxgo
func Join(elem ...string) stringParameters & Attributes
| Name | Type | Description |
|---|---|---|
| elem | ...string | Path elements in order. |
Return Value
string
Combines nonempty path elements and cleans the result using OS separators.
Working Example
Example Snippetgo
package main
import ( "fmt" "os" "path/filepath")
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(filepath.Join("a", "..", "b")) }