windows - Windows 符号链接(symbolic link)和目录之间的区别

标签 windows go symlink

当我试图区分 Windows 符号链接(symbolic link)和目录之间的区别时,我遇到了 Go 的问题。 我用谷歌搜索过,我能找到的就是这个: https://github.com/golang/go/issues/3498#issuecomment-142810957

不幸的是,该项目已关闭并且尚未进行处理。

所以我的问题是,有什么解决方法吗?我尝试使用符号链接(symbolic link)列出路径,但它返回的结果与在空目录上返回的结果相同。

使用 python,我可以做这样的事情:

def test(dir):
    try:
        os.chdir(dir)
    except Exception as e:
        if "[Error 2]" in str(e):
            return False
        else:
            return True

是否有任何 bash 命令可以用来从 go 调用来检测它?

我的想法已经用完了:(

最佳答案

我看到的唯一测试(我刚刚在 Windows 上使用 go 1.5.1 进行了测试)位于 os/os_test.go :

fromstat, err = Lstat(from)
if err != nil {
    t.Fatalf("lstat %q failed: %v", from, err)
}
if fromstat.Mode()&ModeSymlink == 0 {
    t.Fatalf("symlink %q, %q did not create symlink", to, from)
}

它使用 os/#Lstat :

Lstat returns a FileInfo describing the named file.
If the file is a symbolic link, the returned FileInfo describes the symbolic link. Lstat makes no attempt to follow the link.
If there is an error, it will be of type *PathError.

您还可以获取os.Stat()同一文件夹,然后调用os.Samefile() (如 in this test ):

if !SameFile(tostat, fromstat) {
    t.Errorf("symlink %q, %q did not create symlink", to, from)
}

关于windows - Windows 符号链接(symbolic link)和目录之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32753907/

相关文章:

go - ioutils.WriteFile() 不尊重权限

configuration - 为 Tomcat 中的单个目录配置符号链接(symbolic link)

loops - 理解go中的fmt包

c - 如何从 Windows .txt 文件中读取回车符(即 '\r' )

c# - Chrome 是如何实现其标签的?

windows - 防止任务管理器杀死进程,相反

url - 如何使用 go 在 OAuth 中编写重定向 URL

linux - Unix 中的相对符号链接(symbolic link)

python - 有没有办法创建带有参数的符号链接(symbolic link)? (Linux)

sql-server - 在我粘贴一大段代码后 SSMS 崩溃了