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=