html/template: How to bind nested structs from form data?
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 32.7k
- Forks
- 2.7k
- Avg merge
- 9h 35m
- Merged PRs (30d)
- 6
Description
I'm trying to bind form data to nested structs, but I'm not sure if I'm doing it correctly. Is there a recommended way to handle form binding with nested structs using the standard library?
My Use Case
I have a form structure with nested fields that I want to bind to corresponding nested structs:
type UserInfo struct {
Name string `form:"name"`
Sex string `form:"sex"`
}
type UserGroup struct {
Men []UserInfo `form:"men"`
Women []UserInfo `form:"women"`
}
type UserResults struct {
Win UserGroup `form:"win"`
Lose UserGroup `form:"lose"`
}
func handler(w http.ResponseWriter, r *http.Request) {
form := &UserResults{}
if err := c.Bind(form); err != nil {
// error handle
}
// Values are not bound to nested structs
}
What I've Tried
I've attempted to use dot notation in the form field names:
<form method="POST">
<input type="number" name="win.men[0].name" value="john">
<input type="number" name="win.men[0].sex" value="1">
</form>
Questions
- Is there a built-in way to handle nested struct binding with the standard library?
- If yes, what is the correct naming convention for form fields?
- Is there any documentation I might have missed about this topic?
Environment
- Go version: 1.22
Any guidance or best practices would be greatly appreciated. If this functionality isn't currently supported, would it be worth considering as a feature request?
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at the c.Bind call in the example and inspect how the repository handles request form parsing and nested values. Compare that behavior with the standard library's form data conventions, then determine whether the expected naming convention is supported and whether the outcome calls for documentation or a scoped feature proposal.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go
- Domain
- api, backend
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Active
- Clarity
- Needs clarification
- Newbie friendliness
- 35/100