当前位置:网站首页>Golang gets the tag of the loop nested structure
Golang gets the tag of the loop nested structure
2022-07-28 03:30:00 【User nickname cannot be empty】
golang obtain
type Parent struct {
Age int `json:"age"`
City string `json:"city" cond:"title: City "`
Name string `json:"name" cond:"title:name1"`
}
type Son struct {
Parent
Name string `json:"name" cond:"title: name "`
}
Need to be right Son{} Get nested loops cond Data results of the structure of .
- No, cond The neglect of
- The priority of inheritance is still low
Method realization
package main
import (
"errors"
"fmt"
"reflect"
)
func GetAllTags(rt reflect.Type, key string) (tags map[string]reflect.StructField, err error) {
tags = make(map[string]reflect.StructField)
if rt.Kind() != reflect.Struct {
err = errors.New(fmt.Sprintf("bad type not struct %s", rt.Kind()))
return
}
for i := 0; i < rt.NumField(); i++ {
f := rt.Field(i)
// Inlay type
if f.Type.Kind() == reflect.Struct {
var newTags map[string]reflect.StructField
newTags, err = GetAllTags(f.Type, key)
if err != nil {
return
}
for k, v := range newTags {
tags[k] = v
}
}
tag := f.Tag.Get(key)
if tag == "" {
continue
}
jsonName := f.Tag.Get("json")
if jsonName == "" {
err = errors.New(fmt.Sprintf("error %s missing tag JSON", f.Name))
return
}
tags[jsonName] = f
}
return
}
type Parent struct {
Age int `json:"age"`
City string `json:"city" cond:"title: City "`
Name string `json:"name" cond:"title:name1"`
}
type Son struct {
Parent
Name string `json:"name" cond:"title: name "`
}
func main() {
var (
err error
key = "cond"
alls map[string]reflect.StructField
)
{
alls, err = GetAllTags(reflect.TypeOf(Son{
}), key)
if err != nil {
fmt.Println("err1 = ", err.Error())
} else {
fmt.Println("alls = ", alls)
}
}
}
// outputs:
// alls = map[city:{City string json:"city" cond:"title: City " 8 [1] false} name:{Name string json:"name" cond:"title: name " 40 [1] false}]
边栏推荐
- “讳疾忌医”的开源走不远
- SQL Server备份数据库的方法
- Malloc, free, calloc, realloc dynamic memory development functions in dynamic memory management
- Outlook tutorial, how to use color categories and reminders in outlook?
- Summary of redis classic interview questions
- 图文并茂:JVM 内存布局详解
- Redis implements distributed locks
- Shell编写规范和变量
- Engineering Geology Practice - engineering geology problem set
- SSM integration (integrated configuration)
猜你喜欢

xctf攻防世界 Web高手进阶区 PHP2

Outlook tutorial, how to use color categories and reminders in outlook?

MySQL stored procedures use cursors to synchronize data between two tables

RBD块存储设备的扩容以及缩容操作(六)

Contour detection based on OpenCV (3)

When QML uses layout layout, a large number of < unknown file >: QML qquicklayoutattached: binding loop detected for property circular binding warnings appear

When a dialog box pops up, the following form is not available

Win11 how to rename an audio device

颜色的识别方法和探索 基于matlab

"Introduction to engineering electromagnetic field" after class exercises with answers
随机推荐
Redis persistence mechanism
ThreadLocal使用场景
Leetcode 29th day
力扣(LeetCode)208. 实现 Trie (前缀树)(2022.07.27)
Redis实现分布式锁
Color recognition method and exploration based on MATLAB
ASEMI整流桥GBPC3510在直流有刷电机中的妙用
golang gorm查询任意字段的组装方法
IronOCR for .NET 2022.8
Practice of online problem feedback module (16): realize the function of checking details
【Codeforces Round #806 (Div. 4)(A~F)】
颜色的识别方法和探索 基于matlab
C WinForm development: how to add pictures to project resources
Raspberry pie development relay control lamp
同时导出多个excel,并且一个excel中包含多个sheet
Version compatibility issues
Win11输入法的选字框不见了怎么办?
Malloc, free, calloc, realloc dynamic memory development functions in dynamic memory management
如何一键进行重装Win11系统
How to uninstall clean ZABBIX service? (super detailed)