I Spy Code - GO

Shortest path name

Question:

How do you get the shortest path name in golang?

Answer:

The function filepath.Clean() returns the shortest path name equivalent to path by purely lexical processing.

Here is a golang example that shows how to get the shortest pathname in a path:

Source: (example.go)

package main
 
import (
   "fmt"
   "path/filepath"
)
 
func main() {
   clean := filepath.Clean("/home/dennis/../dennis/IdeaProjects/Playground/hello.go")
   fmt.Println("Clean:", clean)
}
 

Output:

$ go run example.go
Clean: /home/dennis/IdeaProjects/Playground/hello.go

References:

https://golang.org/pkg/path/filepath/#Clean

Questions answered by this page:

How to get the shortest path to a file in go lang?