From 36d01f81c0c719176fc01a605c66bb949eb0bc8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=BE=B7=E6=BB=A1?= Date: Sun, 14 Mar 2021 23:19:34 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0structToMap=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- util/struct.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 util/struct.go diff --git a/util/struct.go b/util/struct.go new file mode 100644 index 0000000..7aee6ea --- /dev/null +++ b/util/struct.go @@ -0,0 +1,30 @@ +// Package util... +// +// Description : util ... +// +// Author : go_developer@163.com<张德满> +// +// Date : 2021-03-14 11:11 下午 +package util + +import "encoding/json" + +// StructToMap 结构体转为map +// +// Author : go_developer@163.com<张德满> +// +// Date : 11:12 下午 2021/3/14 +func StructToMap(data interface{}) (map[string]interface{}, error) { + var ( + byteData []byte + err error + result map[string]interface{} + ) + if byteData, err = json.Marshal(data); nil != err { + return nil, err + } + if err = json.Unmarshal(byteData, &result); nil != err { + return nil, err + } + return result, nil +}