1 Star 1 Fork 0

窦雪峰/go-utils

Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
files.go 1.19 KB
Copy Edit Raw Blame History
窦雪峰 authored 2022-05-12 13:47 +08:00 . 重写db、redis
/*
* @Author: i@douxuefeng.cn
* @Date: 2021-04-16 15:25:14
* @LastEditTime: 2022-05-11 17:34:49
* @LastEditors: i@douxuefeng.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
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/douxuefeng/go-utils.git
git@gitee.com:douxuefeng/go-utils.git
douxuefeng
go-utils
go-utils
v1.0.0

Search