Go语言判断文件或文件夹是否存在的方法
作者:phpgo 时间:2024-05-09 14:50:55
本文实例讲述了Go语言判断文件或文件夹是否存在的方法。分享给大家供大家参考,具体如下:
Golang 判断文件是否存在有点怪异,是根据在操作文件时返回的错误信息来判断的,而不能直接根据路径判断
版本1:
func IsExists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
版本2: 简洁版
func IsExist(path string) bool {
_, err := os.Stat(path)
return err == nil || os.IsExist(err)
// 或者
//return err == nil || !os.IsNotExist(err)
// 或者
//return !os.IsNotExist(err)
}
希望本文所述对大家Go语言程序设计有所帮助。
标签:Go语言,文件,文件夹
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
shp2sqlserver 用法简析
2024-01-17 10:37:23
网页设计:把导航系统做薄
2007-12-28 12:02:00
![](https://img.aspxhome.com/file/UploadPic/200712/28/200712281250398s.gif)
Pycharm 如何设置HTML文件自动补全代码或标签
2021-08-12 17:47:15
![](https://img.aspxhome.com/file/2023/3/121473_0s.png)
Python实现对百度云的文件上传(实例讲解)
2022-04-16 03:15:46
![](https://img.aspxhome.com/file/2023/1/107681_0s.jpg)
Goland激活码破解永久版及安装详细教程(亲测可以)
2023-06-18 14:30:43
![](https://img.aspxhome.com/file/2023/7/86287_0s.png)
详解python数据结构之栈stack
2023-02-12 17:48:56
![](https://img.aspxhome.com/file/2023/6/78806_0s.png)
图解Python中浅拷贝copy()和深拷贝deepcopy()的区别
2021-12-09 21:09:39
![](https://img.aspxhome.com/file/2023/0/84970_0s.png)
Python的字符串操作简单实例
2021-03-13 07:16:00
DBA_2PC_PENDING 介绍
2009-02-28 10:59:00
Python利用PyExecJS库执行JS函数的案例分析
2022-10-26 08:53:19
![](https://img.aspxhome.com/file/2023/4/94154_0s.jpg)
Python依赖包迁移到断网环境操作
2021-06-27 06:58:29
Matplotlib 绘制饼图解决文字重叠的方法
2023-06-12 22:23:06
![](https://img.aspxhome.com/file/2023/4/60504_0s.png)
Vue Render函数原理及代码实例解析
2023-07-02 16:34:38
Goland 的安装及激活教程(window、linux下安装)
2024-05-08 10:51:51
![](https://img.aspxhome.com/file/2023/6/127366_0s.png)
python中的计时器timeit的使用方法
2023-04-24 14:50:56
asp如何让浏览器在https和http之间转化?
2010-05-13 16:37:00
ElementUI日期选择器时间选择范围限制的实现
2024-04-09 11:00:28
快速入门python学习笔记
2023-08-25 14:26:29
Spring Boot整合 NoSQL 数据库 Redis详解
2024-01-29 18:16:36
![](https://img.aspxhome.com/file/2023/1/130011_0s.jpg)
404错误伪静态类封装class RewriteBase
2009-06-29 16:19:00