Golang- 获取结构体属性名称

huangapple go评论81阅读模式
英文:

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
}

playground

英文:

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
}

<kbd>playground</kbd>

答案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 (
	&quot;fmt&quot;
	&quot;reflect&quot;
)

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((&amp;MultiQuestions{}).StructAttrName())
}

http://play.golang.org/p/su7VIKXBE2

答案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 (
	&quot;fmt&quot;
	&quot;reflect&quot;

	&quot;github.com/fatih/structure&quot;
)

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((&amp;MultiQuestions{}).StructAttrName())
	fmt.Println(Fields(&amp;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>

huangapple
  • 本文由 发表于 2014年8月5日 05:35:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/25127959.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定