如何获取此结构体中的JSON字段名?
type example struct {
Id int `json:"id"`
CreatedAt string `json:"created_at"`
Tag string `json:"tag"`
Text string `json:"text"`
AuthorId int `json:"author_id"`
}
我尝试使用这个函数打印出字段:
func (b example) PrintFields() {
val := reflect.ValueOf(b)
for i := 0; i < val.Type().NumField(); i++ {
fmt.Println(val.Type().Field(i).Name)
}
}
当然我懂得:
Id
CreatedAt
Tag
Text
AuthorId
但我想要类似这样的东西:
id
created_at
tag
text
author_id