golang:通过reflect取struct的字段长度,手动取结构体tag与name
发布时间:2020-12-16 19:17:50 所属栏目:大数据 来源:网络整理
导读:type Config struct { Listen string `json: "listen" toml: "listen" ` UserAPIURL string `json: "userapisrv" toml: "userAPIURL" ` SecretKey string `json: "secret_key" toml: "secret_key" ` AuthorityUrl string `json: "authorityapisrv" toml: "au
type Config struct {
Listen string `json:"listen" toml:"listen"`
UserAPIURL string `json:"userapisrv" toml:"userAPIURL"`
SecretKey string `json:"secret_key" toml:"secret_key"`
AuthorityUrl string `json:"authorityapisrv" toml:"authority_url"`
ResourceKey string `json:"resource_key" toml:"resource_key"`
}
c := &Config{}
cvalue := reflect.ValueOf(c)
l := cvalue.Elem().NumField()
c := &Config{}
cType := reflect.TypeOf(c)
cValue := reflect.ValueOf(c)
structLen := cValue.Elem().NumField()
structMap := make(map[string]string,structLen)
for i := 0; i < structLen; i++ {
field := cType.Elem().Field(i)
jsonName := field.Tag.Get("json")
structMap[jsonName] = field.Name
} (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |