在Golang中,如何將一個結構體轉成map? 本文介紹兩種方法。第一種是是使用json包解析解碼編碼。第二種是使用反射,使用反射的效率比較高,代碼在這里。如果覺得代碼有用,可以給我的代碼倉庫一個star。
假設有下面的一個結構體
func newUser() User {
name := "user"
MyGithub := GithubPage{
URL: "https://github.com/liangyaopei",
Star: 1,
}
NoDive := StructNoDive{NoDive: 1}
dateStr := "2020-07-21 12:00:00"
date, _ := time.Parse(timeLayout, dateStr)
profile := Profile{
Experience: "my experience",
Date: date,
}
return User{
Name: name,
Github: MyGithub,
NoDive: NoDive,
MyProfile: profile,
}
}
type User struct {
Name string `map:"name,omitempty"` // string
Github GithubPage `map:"github,dive,omitempty"` // struct dive
NoDive StructNoDive `map:"no_dive,omitempty"` // no dive struct
MyProfile Profile `map:"my_profile,omitempty"` // struct implements its own method
}
type GithubPage struct {
URL string `map:"url"`
Star int `map:"star"`
}
type StructNoDive struct {
NoDive int
}
type Profile struct {
Experience string `map:"experience"`
Date time.Time `map:"time"`
}
// its own toMap method
func (p Profile) StructToMap() (key string, value interface{}) {
return "time", p.Date.Format(timeLayout)
}
json包的marshal,unmarshal
先將結構體序列化成[]byte數組,再從[]byte數組序列化成結構體。
data, _ := json.Marshal(user)
m := make(map[string]interface{})
json.Unmarshal(data, m)
優勢
使用簡單 劣勢
效率比較慢
不能支持一些定制的鍵,也不能支持一些定制的方法,例如將struct的域展開等。
使用反射
本文實現了使用反射將結構體轉成map的方法。通過標簽(tag)和反射,將上文示例的newUser()返回的結果轉化成下面的一個map。
其中包含struct的域的展開,定制化struct的方法。
map[string]interface{}{
"name": "user",
"no_dive": StructNoDive{NoDive: 1},
// dive struct field
"url": "https://github.com/liangyaopei",
"star": 1,
// customized method
"time": "2020-07-21 12:00:00",
}
實現思路 源碼解析
1.標簽識別。
使用readTag方法讀取域(field)的標簽,如果沒有標簽,使用域的名字。然后讀取tag中的選項。目前支持3個選項
'-':忽略當前這個域
'omitempty' : 當這個域的值為空,忽略這個域
'dive' : 遞歸地遍歷這個結構體,將所有字段作為鍵
如果選中了一個選項,就講這個域對應的二進制位置為1.。
const (
OptIgnore = "-"
OptOmitempty = "omitempty"
OptDive = "dive"
)
const (
flagIgnore = 1 iota
flagOmiEmpty
flagDive
)
func readTag(f reflect.StructField, tag string) (string, int) {
val, ok := f.Tag.Lookup(tag)
fieldTag := ""
flag := 0
// no tag, use field name
if !ok {
return f.Name, flag
}
opts := strings.Split(val, ",")
fieldTag = opts[0]
for i := 1; i len(opts); i++ {
switch opts[i] {
case OptIgnore:
flag |= flagIgnore
case OptOmitempty:
flag |= flagOmiEmpty
case OptDive:
flag |= flagDive
}
}
return fieldTag, flag
}
2.結構體的域(field)的遍歷。
遍歷結構體的每一個域(field),判斷field的類型(kind)。如果是string,int等的基本類型,直接取值,并且把標簽中的值作為key。
for i := 0; i t.NumField(); i++ {
...
switch fieldValue.Kind() {
case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int, reflect.Int64:
res[tagVal] = fieldValue.Int()
case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint, reflect.Uint64:
res[tagVal] = fieldValue.Uint()
case reflect.Float32, reflect.Float64:
res[tagVal] = fieldValue.Float()
case reflect.String:
res[tagVal] = fieldValue.String()
case reflect.Bool:
res[tagVal] = fieldValue.Bool()
default:
}
}
}
3.內嵌結構體的轉換
如果是結構體,先檢查有沒有實現傳入參數的方法,如果實現了,就調用這個方法。如果沒有實現,就遞歸地調用StructToMap方法,然后根據是否展開(dive),來把返回結果寫入res的map。
for i := 0; i t.NumField(); i++ {
fieldType := t.Field(i)
// ignore unexported field
if fieldType.PkgPath != "" {
continue
}
// read tag
tagVal, flag := readTag(fieldType, tag)
if flagflagIgnore != 0 {
continue
}
fieldValue := v.Field(i)
if flagflagOmiEmpty != 0 fieldValue.IsZero() {
continue
}
// ignore nil pointer in field
if fieldValue.Kind() == reflect.Ptr fieldValue.IsNil() {
continue
}
if fieldValue.Kind() == reflect.Ptr {
fieldValue = fieldValue.Elem()
}
// get kind
switch fieldValue.Kind() {
case reflect.Struct:
_, ok := fieldValue.Type().MethodByName(methodName)
if ok {
key, value, err := callFunc(fieldValue, methodName)
if err != nil {
return nil, err
}
res[key] = value
continue
}
// recursive
deepRes, deepErr := StructToMap(fieldValue.Interface(), tag, methodName)
if deepErr != nil {
return nil, deepErr
}
if flagflagDive != 0 {
for k, v := range deepRes {
res[k] = v
}
} else {
res[tagVal] = deepRes
}
default:
}
}
...
}
// call function
func callFunc(fv reflect.Value, methodName string) (string, interface{}, error) {
methodRes := fv.MethodByName(methodName).Call([]reflect.Value{})
if len(methodRes) != methodResNum {
return "", nil, fmt.Errorf("wrong method %s, should have 2 output: (string,interface{})", methodName)
}
if methodRes[0].Kind() != reflect.String {
return "", nil, fmt.Errorf("wrong method %s, first output should be string", methodName)
}
key := methodRes[0].String()
return key, methodRes[1], nil
}
4.array,slice類型的轉換
如果是array,slice類型,類似地,檢查有沒有實現傳入參數的方法,如果實現了,就調用這個方法。如果沒有實現,將這個field的tag作為key,域的值作為value。
switch fieldValue.Kind() {
case reflect.Slice, reflect.Array:
_, ok := fieldValue.Type().MethodByName(methodName)
if ok {
key, value, err := callFunc(fieldValue, methodName)
if err != nil {
return nil, err
}
res[key] = value
continue
}
res[tagVal] = fieldValue
....
}
5.其他類型
對于其他類型,例如內嵌的map,直接將其返回結果的值。
switch fieldValue.Kind() {
...
case reflect.Map:
res[tagVal] = fieldValue
case reflect.Chan:
res[tagVal] = fieldValue
case reflect.Interface:
res[tagVal] = fieldValue.Interface()
default:
}
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
您可能感興趣的文章:- golang 如何用反射reflect操作結構體
- golang 實現兩個結構體復制字段
- golang通過反射設置結構體變量的值
- Golang空結構體struct{}用途,你知道嗎
- golang修改結構體中的切片值方法
- golang 結構體初始化時賦值格式介紹
- 解決golang結構體tag編譯錯誤的問題