我有一个具有这种结构的复杂对象。
type People struct {
Objectives []string `validate:"required,ValidateCustom" json:"Objectives"`
}我需要使用gopkg.in/go-playground/validator.v9在枚举中测试列表思维
//ValidateCustom -- ValidateCustom
func ValidateCustom(field validator.FieldLevel) bool {
switch strings.ToUpper(field.Field().String()) {
case "emumA":
case "enumB":
return true
default:
return false
}
}这个例子使用了string的概念,但是我如何构建到[]string来迭代呢?
发布于 2020-02-12 20:40:47
我找到了答案。使用切片和接口
//ValidateCustom -- ValidateCustom
func ValidateCustom(field validator.FieldLevel) bool {
inter := field.Field()
slice, ok := inter.Interface().([]string)
if !ok {
return false
}
for _, v := range slice {
switch strings.ToUpper(v) {
case "enumA":
case "enumB":
return true
default:
return false
}
}https://stackoverflow.com/questions/60188591
复制相似问题