Standard library / path

path.Clean()

Simplifies slash-separated paths lexically; it does not inspect a filesystem or resolve symlinks. Example uses a disposable temporary workspace. Expected stdout: '/b/c\n'.

Formal Syntax & Structure

go syntaxgo
func Clean(path string) string

Parameters & Attributes

NameTypeDescription
pathstringPathname interpreted according to this package.

Return Value

string

Simplifies slash-separated paths lexically; it does not inspect a filesystem or resolve symlinks.

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.Clean("/a/../b//c")) }
Go Reference