html/template: How to bind nested structs from form data?
还没有人认领这个 Issue。
评估
调研方向
从示例中的 c.Bind 调用开始,检查 repository 如何处理请求表单解析和嵌套值。将该行为与标准库的表单数据约定进行比较,然后确定是否支持预期的命名约定,以及最终结果需要文档说明还是范围明确的功能提案。
由索引模型根据 Issue 内容生成。
描述
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?
- 主要语言
- Go
- 星标
- 32.7k
- 派生
- 2.8k
- 平均合并
- 9 小时 39 分钟
- 30 天内合并 PR
- 6
贡献指南
这个仓库没有索引到贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
labstack/echo 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 74/100
-
难度 3/5 1-2 天 新手友好度 65/100
-
难度 3/5 1-2 天 新手友好度 68/100
-
难度 3/5 1-2 天 新手友好度 74/100
-
难度 5/5 一周以上 新手友好度 35/100
相似的 Issue
-
难度 1/5 1 小时以内 新手友好度 60/100
github/gh-aw-mcpg#13748 ·
-
agentic-workflows
难度 2/5 1-3 小时 新手友好度 65/100
-
needs-triage
难度 2/5 1-3 小时 新手友好度 75/100
-
难度 2/5 1-3 小时 新手友好度 65/100
-
难度 2/5 1-3 小时 新手友好度 75/100
googleapis/librarian#7670 · 2 条评论 ·