os.open竟然在文件不存在时返回err==nil,亏我那么信任它

weixin_38049686 2019-09-20 09:18:28
```go
package main

import (
"fmt"
"os"
)

func main() {
f := "D:\\cron"
fmt.Println(os.Stat(f))
fmt.Println(os.Open(f))

f = "D:\\con"
fmt.Println(os.Stat(f))
fmt.Println(os.Open(f))
}

结果如下:
<nil> CreateFile D:\cron: The system cannot find the file specified.
<nil> open D:\cron: The system cannot find the file specified.
<nil> CreateFile D:\con: The parameter is incorrect.
&{0xc000086a00} <nil>
```
我就是想读取一个文件,我觉得os.Open()应该在文件不存在时返回错误,这样我就可以不多调一次os.Stat()
然而不知为啥会碰到上面这个问,搞不懂“D:\\con”这个为啥会是参数错误,而且os.Open竟然返回正常。
...全文
18 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复

433

社区成员

发帖
与我相关
我的任务
社区描述
其他技术讨论专区
其他 技术论坛(原bbs)
社区管理员
  • 其他技术讨论专区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧