if _,err := os.Stat("/path/to/whatever"); os.IsNotExist(err) { / path/to/whatever does not exist}/ ^: T0 B. k9 p7 j
检查文件是否存在相当于 Python 的if os.path.exists(filename):: t$ }; J9 z. \
编辑:根据最近的评论 : {4 L: {& g9 H1 I2 k
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}- U4 F! E5 I$ A* I, L. ~( O