1 Star 0 Fork 1

技术狼/go-fun

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
file.go 1.80 KB
一键复制 编辑 原始数据 按行查看 历史
技术狼 提交于 1年前 . no message
package fun
import (
"io"
"io/ioutil"
"os"
"strings"
)
// @title: 文件是否存在
// @param: 文件路径(string)
// @return: 布尔
// @description:
// @date: 2024/6/11 22:32
func FileIsExist(filePath string) (bool, error) {
if _, err := os.Stat(filePath); os.IsNotExist(err) {
return false, err
}
return true, nil
}
// @title: 文件复制
// @param: 文件路径(string)、目标路径(string)
// @return: error
// @description:
// @date: 2024/6/11 22:32
func FileCopy(srcPath, destPath string) error {
srcFile, err := os.Open(srcPath)
if err != nil {
return err
}
defer srcFile.Close()
//
destSplitPathDirs := strings.Split(destPath, "/") //分割path目录
destSplitPath := "" //检测时候存在目录
for index, dir := range destSplitPathDirs {
if index < len(destSplitPathDirs)-1 {
destSplitPath = destSplitPath + dir + "/"
is, _ := DirIsExist(destSplitPath)
if is == false {
err := os.Mkdir(destSplitPath, os.ModePerm)
if err != nil {
return err
}
}
}
}
//
dstFile, err := os.Create(destPath)
if err != nil {
return err
}
defer dstFile.Close()
_, err = io.Copy(dstFile, srcFile)
return err
}
// @title: 获取文件内容
// @param: 路径(string)
// @return: 文件流([]byte), error
// @description:
// @date: 2024/6/11 22:32
func FileGetContents(filePath string) ([]byte, error) {
file, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer file.Close()
bytes, err := ioutil.ReadAll(file)
if err != nil {
return nil, err
}
return bytes, nil
}
// @title: 文件写入内容
// @param: 路径(string),内容(string)
// @return: error
// @description:
// @date: 2024/6/11 22:32
func FilePutContents(filePath string, content string) error {
return ioutil.WriteFile(filePath, []byte(content), 0644)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/jishulangcom/go-fun.git
git@gitee.com:jishulangcom/go-fun.git
jishulangcom
go-fun
go-fun
v0.0.4

搜索帮助