登录
首页 >  Golang >  Go问答

使用 os.Open 时如何处理无此文件情况

来源:stackoverflow

时间:2024-03-06 21:54:19 444浏览 收藏

积累知识,胜过积蓄金银!毕竟在Golang开发的过程中,会遇到各种各样的问题,往往都是一些细节知识点还没有掌握好而导致的,因此基础知识点的积累是很重要的。下面本文《使用 os.Open 时如何处理无此文件情况》,就带大家讲解一下知识点,若是你对本文感兴趣,或者是想搞懂其中某个知识点,就请你继续往下看吧~

问题内容

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))
}

result:
 CreateFile D:\cron: The system cannot find the file specified.
 open D:\cron: The system cannot find the file specified.
 CreateFile D:\con: The parameter is incorrect.
&{0xc000086a00} 

我认为当文件不存在时,os.open应该返回 err != nil

但我想知道为什么“d:\con”返回 err == nil


解决方案


您使用 Windows 吗? “con”是保留文件名,是 DOS 时代的遗物。

以上就是《使用 os.Open 时如何处理无此文件情况》的详细内容,更多关于的资料请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>