我目前正在调查 go 以了解更多相关信息,但我已经找到了进入我无法突破的墙的方法。我想做的是获取目录或文件的用户输入等:/Users/me/Documents/test.sql 并将其复制到另一个目录等:/usr/local/share/myprogram
问题是 os.Lstat 在我尝试的任何事情中都找不到它...
我知道 test.sql 存在,如果我在终端中使用 open/Users/me/Documents/test.sql,Go 不会说找不到我设法打开它,所以它就在那里.
为什么会这样?这是 go 的东西吗,路径设置为只在工作目录中查找文件?我该如何克服这个问题?
os.Lstat("/Users/me/Documents/test.sql")
我也尝试过使用文件路径
toPath, _ := filepath.Abs("/Users/me/Documents/test.sql")
os.Lstat(toPath)
我希望找到这个文件,但它没有
最佳答案
docs说:
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.
这是您可以尝试的示例:
package main
import (
"fmt"
"log"
"os"
)
func main() {
desc, err := os.Lstat("/Users/me/Documents/test.sql")
if err != nil {
// prints the error and stop
log.Fatal(err)
}
// prints the filename
fmt.Println(desc.Name())
}
关于go - 在磁盘上找不到确实存在的文件或目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55798274/