【发布时间】:2017-04-13 09:18:12
【问题描述】:
这个结构的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
【问题讨论】:
-
查看这个答案(它包含一个获取标签值的示例):What are the use(s) for tags in Go?