golang defer 关闭文件 报错file may have nil or other unexpected value as its corresponding error

错误实例:

file,err := os.Open("xxx.txt")
defer file.Close()
if err != nil {
       return err
}

初学者很多这样写,但是这样会报错

file’ may have ‘nil’ or other unexpected value as its corresponding error variable may be not ‘nil’

意思是,file可能是nil,调用close会发生panic。正确的做法:

file,err := os.Open("xxx.txt")
if err != nil {
    return
}
defer file.Close()

但是通过看源码,发现file实现的close函数,已经对file等于nil的情况做处理返回ErrInvalid了,所以实际是不会发生panic的,但是goland ide提示以上的报错是因为一般情况下,对于在检测err是否不为nil前做close处理确实会发生panic的,因为对象时nil时做调用

go1.13.7 /Go/src/os/file_unix.go:229

// Close closes the File,rendering it unusable for I/O.
// On files that support SetDeadline,any pending I/O operations will
// be canceled and return immediately with an error.
// Close will return an error if it has already been called.
func (f *File) Close() error {
    if f == nil {
        return ErrInvalid
    }
    return f.file.close()
}

源码里这种处理,可能是觉得file实现的close是高频调用函数,怕新手在调用时不符合规范而造成panic发生,因此这里做了特殊的处理。

 

 

相关文章

功能概要:(目前已实现功能)公共展示部分:1.网站首页展示...
大体上把Python中的数据类型分为如下几类: Number(数字) ...
开发之前第一步,就是构造整个的项目结构。这就好比作一幅画...
源码编译方式安装Apache首先下载Apache源码压缩包,地址为ht...
前面说完了此项目的创建及数据模型设计的过程。如果未看过,...
python中常用的写爬虫的库有urllib2、requests,对于大多数比...