reflect: NumField of non-struct type,这是怎么回事?

我定义了一个user model:

type User struct {
    Id            uint      `model:"id"`
    Username      string    `model:"username"`
    MobileNo      string    `model:"mobile_no"`
    Password      string    `model:"password"`
    Email         string    `model:"email"`
    EmailVerified uint8     `model:"email_verified"`
    CreateTime    string    `model:"create_time"`
    UpdateTime    string    `model:"uptime_time"`
}

func (user *User) TableName() string {
    return "user"
}

这个user model有实现一个baseModel接口。

然后我查询数据:

user := &model.User{}
db.BuildCommand(user).FindOne()

BuildCommand:

type Command struct {
    model  BaseModel
    sql    string  //最终的SQL语句
    selt   string  //sql 的select
    from   string
    where  string
    limit  int
    offset int
}

var c *Command

func BuildCommand(m BaseModel) *Command {
    if c == nil {
        c = &Command{
            model: m,
        }
    } else {
        c.model = m
    }
    return c
}

然后我在把查询出来的数据写到user model里面的时候,反射user model结构报错了。
panic serving [::1]:50250: reflect: NumField of non-struct type

//....
//这里的q就是上门的*Command
refValue := reflect.ValueOf(q.model)
refType := refValue.Type()
//....
//就下面这一行报错的。
log.Fatalln(refType.NumField())
//.....

这是怎么回事?

阅读 15.5k
1 个回答
refType := reflect.TypeOf(q.model)
refValue := reflect.ValueOf(q.model)

log.Fatalln(refType.NumField())
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题