how to ignore a field in a struct when some data is being parsed into the struct

tags: learning go programming

content

  • im seeing this code:
type userSignupForm struct {
    Name string `form:"name"`
    Email string `form:"email"`
    Password string `form:"password"`
    validator.Validator `form:"-"`
}
  • the form is next to the fields in the struct is called struct tags

  • struct tags tell parsing library how to map some data to struct

    • in this case, it tells Go how to map form fields to userSignupForm struct fields
  • form:"-" tells the parser to ignore this field

  • this is because validator is just something that’s being attached to userSignupForm model, but it’s not a data field that’s coming from the form

    • it’s not meant to be populated from the form
    • it’s internally used in the application’s logic

up

down

reference