I Spy Code - GO

Absolute path from relative path

Question:

How do get the absolute path from a relative path in golang?

Answer:

The function filepath.Abs() returns an absolute representation of path.

Here is a golang example that shows how get the full (absolute) path from a relative path:

Source: (example.go)

package main
 
import (
   "fmt"
   "path/filepath"
)
 
func main() {
   abs,err := filepath.Abs("./hello.go")
   if err == nil {
      fmt.Println("Absolute:", abs)
   }
}
 

Output:

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

References:

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

Questions answered by this page:

How do you get the full from a relative path in golang?
Generating an absolute path in golang.