Go 标准库不专门用于检查文件是否存在函数(如 Python 的os.path.exists)。用什么方法来做到这一点? y0 [- I1 ?4 f( [8 @+ E
( t. i/ Y% o7 P1 }8 F N. F! x/ Q 解决方案: 6 v1 S' h( P/ c+ U 检查文件是否不存在相当于 Python 的if not os.path.exists(filename): / ]4 h9 m# w( y6 d( c
if _,err := os.Stat("/path/to/whatever"); os.IsNotExist(err) { / path/to/whatever does not exist} 8 I, O# A5 U, W+ I' H
检查文件是否存在相当于 Python 的if os.path.exists(filename):$ M, O i( D/ v. R1 [( T' |) r
编辑:根据最近的评论2 v3 p6 d7 R2 f2 K; e
if _,err := os.Stat("/path/to/whatever"); err == nil { / path/to/whatever exists} else if os.IsNotExist(err) { / path/to/whatever does *not* exist} else { / Schrodinger: file may or may not exist. See err for details. // Therefore,do *NOT* use !os.IsNotExist(err) to test for file existence} # o8 S( k, D; p1 m' y2 d