2

I have the following route:

m.Post("/users", binding.Bind(models.User{}), func(user models.User, r render.Render)

And I receive the following error message when I try to do a Post request:

"PANIC: reflect.Value.Interface: cannot return value obtained from unexported field or method"

type User struct {
    id         int
    UUID       string    `json:"uuid"`
    Username   string    `json:"userName" form:"userName" binding:"required"`
    Firstname  string    `json:"firstName" form:"Firstname" binding:"required`
    Lastname   string    `json:"lastName" form:"Lastname" binding:"required`
    Email      string    `json:"email" form:"Email" binding:"required`
    IsActive   bool      `json:"isActive"`
    DateJoined time.Time `json:"dateJoined"`
}

Does anyone have any idea?

Nikolay Kostov
  • 16,433
  • 23
  • 85
  • 123

1 Answers1

0

I know the problem now.

The problem is the attribute "id" of the User struct. If "id" is renamed to "Id" the binding process works perfectly.

With tag form:"-" you can ignore struct attributes.

type User struct {
    id         int       `form:"-"`
    UUID       string    `json:"uuid"`
    Username   string    `json:"userName" form:"userName" binding:"required"`
    Firstname  string    `json:"firstName" form:"Firstname" binding:"required`
    Lastname   string    `json:"lastName" form:"Lastname" binding:"required`
    Email      string    `json:"email" form:"Email" binding:"required`
    IsActive   bool      `json:"isActive"`
    DateJoined time.Time `json:"dateJoined"`
}
topskip
  • 16,207
  • 15
  • 67
  • 99