I Spy Code - GO

Evaluate symbolic links

Question:

How to get the real path of a symbolic link in golang?

Answer:

The function filepath.EvalSymlinks() returns the path name after the evaluation of any symbolic links.

Here is a golang example that returns the path of a symbolic link:

Source: (example.go)

package main
 
import (
   "fmt"
   "path/filepath"
)
 
func main(){
 
   path, _ := filepath.EvalSymlinks("/tmp/myfile2")
   fmt.Println("Path", path)
}
 

Output:

$ go run example.go
/tmp/myfile1

$ ls -l /tmp/myfile*
-rw-r--r--  1 dennis  wheel  0 Feb 13 15:31 /tmp/myfile1
lrwxr-xr-x  1 dennis  wheel  7 Feb 13 15:31 /tmp/myfile2 -> myfile1

References:

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

Questions answered by this page:

How to get the real file name of a symbolic link in golang?
How to get the real path of a symbolic link in golang?