1 回答

TA贡献2012条经验 获得超12个赞
我不完全理解你的问题,但你正在做的事情可以很容易地缩短而不影响功能:
reflectItem := reflect.ValueOf(dataStruct)
subItem := reflectItem.FieldByName(subItemKey)
switch subItemVal := subItem.(type) {
case string:
searchData = bson.D{{"data." +
strings.ToLower(subItemKey), subItemVal}}
case int64:
searchData = bson.D{{"data." +
strings.ToLower(subItemKey), subItemVal}}
}
但除此之外,我认为在您的情况下根本不需要类型断言。这也应该有效:
reflectItem := reflect.ValueOf(dataStruct)
subItem := reflectItem.FieldByName(subItemKey)
searchData = bson.D{{"data."+strings.ToLower(subItemKey), subItem.Interface())
- 1 回答
- 0 关注
- 100 浏览
添加回答
举报