golang symbolic links_第1张图片

package main

import (
    "fmt"
    "os"
    "path/filepath"
)

func main() {
    arguments := os.Args
    if len(arguments) == 1 {
        fmt.Println("Please provide an argument!")
        os.Exit(1)
    }
    filename := arguments[1]

    fileInfo, err := os.Lstat(filename)
    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }
    if fileInfo.Mode()&os.ModeSymlink != 0 {
        fmt.Println(filename, "is a symbolic link")
        realPath, err := filepath.EvalSymlinks(filename)
        if err == nil {
            fmt.Println("Path:", realPath)
        }
    }
}