From b5ae4cf80989bd0e6f12c48c77d4d3c054a988da 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:24:29 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E8=A7=A3=E6=9E=90ini?= =?UTF-8?q?=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- file.go | 16 ++++++++++++++++ go.mod | 2 ++ go.sum | 2 ++ 3 files changed, 20 insertions(+) diff --git a/file.go b/file.go index 38201c2..41c0caf 100644 --- a/file.go +++ b/file.go @@ -12,6 +12,8 @@ import ( "os" "strings" + "github.com/go-ini/ini" + "github.com/pkg/errors" yml "gopkg.in/yaml.v3" ) @@ -81,6 +83,18 @@ func (f *file) ReadJSONContent(filePath string, result interface{}) error { return JSON.UnmarshalWithNumber(fileContent, result) } +// ReadIniContent 读取并解析ini文件 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 13:21 2022/7/2 +func (f *file) ReadIniContent(filePath string, result interface{}) error { + if nil == result { + return errors.New("接收读取结果的数据指针为NIL") + } + return ini.MapTo(result, filePath) +} + // ReadAnyFileContent 读取任意类型的文件并解析 // // Author : go_developer@163.com<白茶清欢> @@ -103,6 +117,8 @@ func (f *file) ReadAnyFileContent(filePath string, receiver interface{}) error { fallthrough case "yaml": parseFunc = f.ReadYmlContent + case "ini": + parseFunc = f.ReadIniContent default: return errors.New(fileExt + " 暂不支持当前格式的文件解析") } diff --git a/go.mod b/go.mod index 65f9136..5019803 100644 --- a/go.mod +++ b/go.mod @@ -7,3 +7,5 @@ require ( github.com/spaolacci/murmur3 v1.1.0 gopkg.in/yaml.v3 v3.0.1 ) + +require github.com/go-ini/ini v1.66.6 // indirect diff --git a/go.sum b/go.sum index 8b90ace..bfc81d1 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +github.com/go-ini/ini v1.66.6 h1:h6k2Bb0HWS/BXXHCXj4QHjxPmlIU4NK+7MuLp9SD+4k= +github.com/go-ini/ini v1.66.6/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=