我正在尝试创建一个函数,该函数将从预定义的数组中生成 if 条件。
例如:
package errors
type errorCase struct {
// This is the field I need to get in another struct
Field string
// The comparison operator
TestOperator string
// The value that the expected one should not with equal...
WrongValue interface{}
}
var ErrorCases = []*errorCase{ {
"MinValue",
"<",
0,
}, {
"MaxValue",
"==",
0,
}}
实际上,我用 for 循环创建了一个新函数,该函数迭代所有这些“错误情况”
func isDirty(questionInterface models.QuestionInterface) bool {
for _, errorCase := range errors.ErrorCases {
s := reflect.ValueOf(&questionInterface).Elem()
value := s.Elem().FieldByName(errorCase.Field)
// At this point I need to create my if condition
// to compare the value of the value var and the wrong one
// With the given comparison operator
}
// Should return the comparison test value
return true
}
是否可以创建这样的 if 条件?使用反射包?
我认为这是可能的,但我不知道应该从哪里开始。
jeck猫
相关分类