gpt4 book ai didi

go - Golang检查不存在的路径是否为目录

转载 作者:行者123 更新时间:2023-12-01 22:38:47 25 4
gpt4 key购买 nike

假设我有一个路径C:\Users\foo\bar,我想检查它是否是一个目录(在示例中,该路径将是一个目录)。即使目录在磁盘上不存在,在Go中有内置的方法可以做到这一点吗?

最佳答案

您可以使用os.Stat做到这一点:

fi, err:=os.Stat(path)
if err!=nil {
// Does not exist, or permissions issue, etc.
pathError:=err.(*os.PathError)
// get detalils of error if necessary
} else if fi.IsDir() {
// Directory
}

您无法检查不存在的路径是否可以是目录。在您的示例中, bar可以是文件或目录。

关于go - Golang检查不存在的路径是否为目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60142166/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com