1 Star 0 Fork 1

Yietion / go-utils

forked from 可乐烛光烟 / go-utils 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
files.go 1.18 KB
一键复制 编辑 原始数据 按行查看 历史
可乐烛光烟 提交于 2022-11-09 00:21 . Initial commit
/*
* @Author: i@rysa.cn
* @Date: 2021-04-16 15:25:14
* @LastEditTime: 2022-05-11 17:34:49
* @LastEditors: i@rysa.cn
* @Description:
* @FilePath: \go-utils\files.go
*/
package go_utils
import (
"io/ioutil"
"os"
)
type File struct {
}
var NewFile = newFile()
func newFile() *File {
return &File{}
}
// 判断所给路径文件/文件夹是否存在(返回true是存在)
func (f *File) IsExist(path string) bool {
_, err := os.Stat(path) // os.Stat获取文件信息
if err != nil {
return os.IsExist(err)
}
return true
}
// 调用os.MkdirAll递归创建文件夹
func (f *File) CreateFile(filePath string) error {
if !f.IsExist(filePath) {
err := os.MkdirAll(filePath, os.ModePerm)
return err
}
return nil
}
// 将数据写入文件中,如果文件已存在,覆盖
func (f *File) WriteFile(filename string, data []byte, perm os.FileMode) error {
return ioutil.WriteFile(filename, data, perm)
}
// 向文件追加内容
func (f *File) AppendFile(filename string, data []byte) error {
file, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY, 0600)
if err != nil {
return err
}
defer file.Close()
if _, err = file.Write(data); err != nil {
return err
}
return nil
}
1
https://gitee.com/yietion/go-utils.git
git@gitee.com:yietion/go-utils.git
yietion
go-utils
go-utils
master

搜索帮助