From fc651441ab844b42480fd359c7c87c80e62bc53d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Sat, 2 Jul 2022 13:18:40 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E4=BB=BB=E6=84=8F=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E7=9A=84=E6=96=87=E4=BB=B6=E8=A7=A3=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- file.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/file.go b/file.go index 20e70f3..38201c2 100644 --- a/file.go +++ b/file.go @@ -81,6 +81,34 @@ func (f *file) ReadJSONContent(filePath string, result interface{}) error { return JSON.UnmarshalWithNumber(fileContent, result) } +// ReadAnyFileContent 读取任意类型的文件并解析 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 13:11 2022/7/2 +func (f *file) ReadAnyFileContent(filePath string, receiver interface{}) error { + fileInfoArr := strings.Split(filePath, ".") + if len(fileInfoArr) < 2 { + return errors.New("未知的文件类型") + } + var ( + parseFunc func(filePath string, receiver interface{}) error + ) + + fileExt := strings.ToLower(fileInfoArr[len(fileInfoArr)-1]) + switch fileExt { + case "json": + parseFunc = f.ReadJSONContent + case "yml": + fallthrough + case "yaml": + parseFunc = f.ReadYmlContent + default: + return errors.New(fileExt + " 暂不支持当前格式的文件解析") + } + return parseFunc(filePath, receiver) +} + // ReadFileContent 读取文件内容 // // Author : go_developer@163.com<白茶清欢>