go

编码习惯&标准库

1.defer的应用,close文件

Posted by Liangjf on March 30, 2020

编码习惯&标准库[1.defer的应用,close文件]

编码习惯&标准库 是一个系列文章,会不定时更新,主要是记录平时对go的编码,特性,标准库的学习研究。

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发生,因此这里做了特殊的处理。