How to check if a file exists in Go?

前端 未结 11 1788
陌清茗
陌清茗 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:53

        _, err := os.Stat(file)
        if err == nil {
            log.Printf("file %s exists", file)
        } else if os.IsNotExist(err) {
            log.Printf("file %s not exists", file)
        } else {
            log.Printf("file %s stat error: %v", file, err)
        }
    

提交回复
热议问题