1 Star 1 Fork 0

kade/library

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
read.go 1.28 KB
一键复制 编辑 原始数据 按行查看 历史
kadegolang 提交于 2023-12-21 17:36 . read
package file
import (
"encoding/json"
"io"
"os"
"sigs.k8s.io/yaml"
)
func ReadFile(path string) (string, error) {
fs, err := os.Open(path)
if err != nil {
return "", err
}
content, err := io.ReadAll(fs)
if err != nil {
return "", err
}
return string(content), nil
}
func ReadContentFile(filepath string) ([]byte, error) {
fd, err := os.Open(filepath)
if err != nil {
return nil, err
}
defer fd.Close()
payload, err := io.ReadAll(fd)
if err != nil {
return nil, err
}
return payload, nil
}
func MustReadContentFile(filepath string) string {
content, err := ReadContentFile(filepath)
if err != nil {
panic(err)
}
return string(content)
}
func ReadYamlFile(filepath string, v any) error {
content, err := ReadContentFile(filepath)
if err != nil {
return err
}
return yaml.Unmarshal(content, v)
}
func MustReadYamlFile(filepath string, v any) {
err := ReadYamlFile(filepath, v)
if err != nil {
panic(err)
}
}
func MustReadJsonFile(filepath string, v any) {
err := ReadJsonFile(filepath, v)
if err != nil {
panic(err)
}
}
func ReadJsonFile(filepath string, v any) error {
fd, err := os.Open(filepath)
if err != nil {
return err
}
defer fd.Close()
payload, err := io.ReadAll(fd)
if err != nil {
return err
}
return json.Unmarshal(payload, v)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/go-kade/library.git
git@gitee.com:go-kade/library.git
go-kade
library
library
v1.1.4

搜索帮助