How to check if a file exists in Go?

前端 未结 11 1791
陌清茗
陌清茗 2020-11-30 16:23

Go\'s standard library does not have a function solely intended to check if a file exists or not (like Python\'s os.path.exists). What is the idiomatic way

11条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-30 16:32

    Best way to check if file exists:

    if _, err := os.Stat("/path/to/file"); err == nil || os.IsExist(err) {
        // your code here if file exists
    }
    

提交回复
热议问题