英文:
Golang variable struct field
问题
这可能需要一些思考,但我不确定。我正在尝试循环遍历一个结构体中的必需字段数组。如果其中任何字段为nil,我想要抛出一个错误。我已经掌握了基本的形式,但我意识到我不知道如何通过变量传递结构体字段名。
假设你有一个名为EmailTemplate的结构体,它有一个名为template_id的字段。
在这种情况下,我想知道EmailTemplate.TemplateId是否为nil。
emailDef.Fields是一个字符串数组["TemplateId"]。
我想要检查这些字段是否在EmailTemplate结构体中,并且是否为nil。
for field := range emailDef.Fields {
fmt.Println(emailDef.Fields[field])
if EmailTemplate.[emailDef.Fields[field]] == nil {
missingField := true
}
}
这是我思考的方向,但我知道这是错误的,因为结构体不是一个数组。emailDef.Fields[field]相当于TemplateId。
英文:
This may require reflection but I'm not sure. I am trying to loop through an array of required fields in a struct. If any of those fields are nil I want to throw an error essentially. I've got the basic form down but I realized I don't know how in Go to pass a struct field name via a variabel
imagine you have a struct called EmailTemplate and it has a field called template_id
In this case I want to know if EmailTemplate.TemplateId is nil
emailDef.Fields is a string array ["TemplateId"]
I want to check if those fields are in the EmailTemplate struct and if they are nil
for field := range emailDef.Fields {
fmt.Println(emailDef.Fields[field])
if EmailTemplate.[emailDef.Fields[field]] == nil {
missingField := true
}
}
is along the lines of what I am thinking but I know that is wrong as a struct isn't an array. emailDef.Fields[field] would be equivalent to TemplateId
答案1
得分: 1
你的循环部分对我来说不太有意义,所以我会给出一个通用的示例,其中有一个名为field
的字符串字段。如果你有一个要检查的字段的切片或数组,你可以使用当前值field
进行遍历。
import "reflect"
st := reflect.TypeOf(EmailTemplate)
v, ok := st.FieldByName(field)
if ok {
// EmailTemplate上存在该字段,现在检查它是否为nil
if v.IsNil() {
// 实例EmailTemplate上的字段field为nil,做一些操作
}
}
假设你有一系列需要检查是否都非nil的字段,那么只需添加一个循环,如下所示:
for field := range requiredFields {
st := reflect.TypeOf(EmailTemplate)
v, ok := st.FieldByName(field)
if ok {
// EmailTemplate上存在该字段,现在检查它是否为nil
if v.IsNil() {
// 实例EmailTemplate上的字段field为nil,做一些操作
// 可能会引发错误,因为字段为nil
}
} else {
// 根本找不到该字段,可能是时候引发错误了
}
}
reflect
包的文档在这里:https://golang.org/pkg/reflect/
英文:
Your loop stuff doesn't make a whole lot of sense to me so I'll give a general example with a single field in a string called field
. If you have a slice or array of fields you want to check for then you'll want to range over that using the current value for field
.
import "reflect"
st := reflect.TypeOf(EmailTemplate)
v, ok := st.FieldByName(field)
if ok {
// field existed on EmailTemplate, now check if it's nil
if v.IsNil() {
// the field field on instance EmailTemplate was nil, do something
}
}
Now assuming you have a list of fields you need to check are all non-nil, then just add a loop like so;
for field := range requiredFields {
st := reflect.TypeOf(EmailTemplate)
v, ok := st.FieldByName(field)
if ok {
// field existed on EmailTemplate, now check if it's nil
if v.IsNil() {
// the field field on instance EmailTemplate was nil, do something
// maybe raise error since the field was nil
} else {
//the field wasn't found at all, probably time to raise an error
}
}
}
reflect package docs are here; https://golang.org/pkg/reflect/
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论