
上QQ阅读APP看书,第一时间看更新
How to do it...
- Open the console and create the folder chapter01/recipe05.
- Navigate to the directory.
- Create the main.go file with the following content:
package main
import (
"fmt"
"os"
"path/filepath"
)
func main() {
ex, err := os.Executable()
if err != nil {
panic(err)
}
// Path to executable file
fmt.Println(ex)
// Resolve the direcotry
// of the executable
exPath := filepath.Dir(ex)
fmt.Println("Executable path :" + exPath)
// Use EvalSymlinks to get
// the real path.
realPath, err := filepath.EvalSymlinks(exPath)
if err != nil {
panic(err)
}
fmt.Println("Symlink evaluated:" + realPath)
}
- Build the binary by the command go build -o binary.
- Execute the binary by the Terminal call ./binary.
- See the output. It should display the absolute path on your machine:
