当我试图区分 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 aFileInfo
describing the named file.
If the file is a symbolic link, the returnedFileInfo
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/