From a200850a5705d98922336ec624758c0166783f22 Mon Sep 17 00:00:00 2001 From: listen <494177603@qq.com> Date: Fri, 17 Dec 2021 20:23:53 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A0=E5=85=A5interface=E8=BD=ACmap[string]?= =?UTF-8?q?string?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- map.go | 47 +++++++++++++++++++++++++++++++++++++---------- 1 file changed, 37 insertions(+), 10 deletions(-) diff --git a/map.go b/map.go index 404d585..0eedd6c 100644 --- a/map.go +++ b/map.go @@ -97,32 +97,59 @@ func MapStringToInterface(src map[string]string) map[string]interface{} { } //将interface转成map[string]interface -func InterfaceToMapInterface(data interface{})(map[string]interface{},error){ - dataJson,err := json.Marshal(data) +func InterfaceToMapInterface(data interface{}) (map[string]interface{}, error) { + dataJson, err := json.Marshal(data) if err != nil { - return nil,err + return nil, err } var returnData map[string]interface{} - err = json.Unmarshal(dataJson,&returnData) + err = json.Unmarshal(dataJson, &returnData) - return returnData,nil + return returnData, nil } +//将interface转成map[string]string +func InterfaceToMapString(data interface{}) (map[string]string, error) { + dataJson, err := json.Marshal(data) + + if err != nil { + return nil, err + } + + var returnData map[string]string + err = json.Unmarshal(dataJson, &returnData) + + return returnData, nil +} //将interface转成[]map[string]interface -func InterfaceToMapInterfaceArr(data interface{})([]map[string]interface{},error){ - dataJson,err := json.Marshal(data) +func InterfaceToMapInterfaceArr(data interface{}) ([]map[string]interface{}, error) { + dataJson, err := json.Marshal(data) if err != nil { - return nil,err + return nil, err } var returnData []map[string]interface{} - err = json.Unmarshal(dataJson,&returnData) + err = json.Unmarshal(dataJson, &returnData) + + return returnData, nil +} + +//将interface转成[]map[string]string +func InterfaceToMapStringArr(data interface{}) ([]map[string]string, error) { + dataJson, err := json.Marshal(data) + + if err != nil { + return nil, err + } + + var returnData []map[string]string + err = json.Unmarshal(dataJson, &returnData) - return returnData,nil + return returnData, nil } /**