英文:
Golang- Getting struct attribute name
问题
我想使用反射包(reflect package)返回一个结构体属性的名称。到目前为止,我有以下代码:
type MultiQuestions struct {
QuestionId int64
QuestionType string
QuestionText string
}
func (q *MultiQuestions) StructAttrName() string {
return reflect.ValueOf(q).Elem().Field(0).Name
}
然而,这给我一个错误信息:reflect.ValueOf(q).Elem().Field(0).Name undefined (type reflect.Value has no field or method Name)
。
我尝试将其转换为StructField,但也没有成功。我该如何获取结构体的名称?
在这种情况下,我感兴趣的名称是QuestionId、QuestionType和QuestionText。
英文:
I want to return the name of a struct attribute using the reflect package. So far I have:
type MultiQuestions struct {
QuestionId int64
QuestionType string
QuestionText string
}
func (q *MultiQuestions) StructAttrName() string {
return reflect.ValueOf(q).Elem().Field(0).Name
}
However, this gives me a error reflect.ValueOf(q).Elem().Field(0).Name undefined (type reflect.Value has no field or method Name)
I tried casting to StructField but that didn't work either. How do I get at the name of Struct?
In this case, the names I am interested in are QuestionId, QuestionType and QuestionText.
答案1
得分: 5
你需要对Type
进行操作,而不是Value
。
func (q *MultiQuestions) StructAttrName() string {
return reflect.Indirect(reflect.ValueOf(q)).Type().Field(0).Name
}
英文:
You need to operate on the Type
not the Value
func (q *MultiQuestions) StructAttrName() string {
return reflect.Indirect(reflect.ValueOf(q)).Type().Field(0).Name
}
答案2
得分: 0
使用类型:
package main
import (
"fmt"
"reflect"
)
type MultiQuestions struct {
QuestionId int64
QuestionType string
QuestionText string
}
func (q *MultiQuestions) StructAttrName() string {
return reflect.TypeOf(q).Elem().Field(0).Name
}
func main() {
fmt.Println((&MultiQuestions{}).StructAttrName())
}
英文:
Use the Type:
package main
import (
"fmt"
"reflect"
)
type MultiQuestions struct {
QuestionId int64
QuestionType string
QuestionText string
}
func (q *MultiQuestions) StructAttrName() string {
return reflect.TypeOf(q).Elem().Field(0).Name
}
func main() {
fmt.Println((&MultiQuestions{}).StructAttrName())
}
答案3
得分: 0
你还可以考虑使用github.com/fatih/structure
中定义的实用函数,比如Fields(s interface{}) []string
,它可以用于指针或对象,包括struct
内部的struct
字段。
package main
import (
"fmt"
"reflect"
"github.com/fatih/structure"
)
type MultiQuestions struct {
QuestionId int64
QuestionType string
QuestionText string
SubMQ SubMultiQuestions
}
type SubMultiQuestions struct{}
func (q *MultiQuestions) StructAttrName() string {
return reflect.Indirect(reflect.ValueOf(q)).Type().Field(0).Name
}
func main() {
fmt.Println((&MultiQuestions{}).StructAttrName())
fmt.Println(structure.Fields(&MultiQuestions{}))
fmt.Println(structure.Fields(MultiQuestions{}))
}
输出结果:
SubMQ
[QuestionId QuestionType QuestionText SubMQ]
[QuestionId QuestionType QuestionText SubMQ]
在这个kbd中可以看到完整的示例。
英文:
You also can consider to utility functions defined in github.com/fatih/structure
, like the Fields(s interface{}) []string
one, which work on pointers or objects, including struct
fields within the struct
.
package main
import (
"fmt"
"reflect"
"github.com/fatih/structure"
)
type MultiQuestions struct {
QuestionId int64
QuestionType string
QuestionText string
SubMQ SubMultiQuestions
}
type SubMultiQuestions struct{}
func (q *MultiQuestions) StructAttrName() string {
return reflect.Indirect(reflect.ValueOf(q)).Type().Field(0).Name
}
func main() {
fmt.Println((&MultiQuestions{}).StructAttrName())
fmt.Println(Fields(&MultiQuestions{}))
fmt.Println(Fields(MultiQuestions{}))
}
Output:
SubMQ
[QuestionId QuestionType QuestionText SubMQ]
[QuestionId QuestionType QuestionText SubMQ]
See a full example in this <kbd>play.golang.org</kbd>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论