Golang 解析形式
Golang parse form
如果我有以下表单设置:
{{ range $key, $value := .Scores }}
<input id="{{$value.Id}}_rating__1" type="radio" name="rating[{{$value.Id}}]" value="-1">
<input id="{{$value.Id}}_rating__0" type="radio" name="rating[{{$value.Id}}]" value="0">
<input id="{{$value.Id}}_rating__2" type="radio" name="rating[{{$value.Id}}]" value="+1">
{{ end }}
我怎样才能正确提取该数据?知道那里.Scores
可以包含多个结构
func categoryViewSubmit(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
log.Fatal(err)
}
fmt.Println("POST")
fmt.Printf("%+v\n", r.Form()) // annot call non-function r.Form (type url.Values)
fmt.Printf("%+v\n", r.FormValue("rating")) // Returns nothing
}
表单键看起来像 rating[id]
,其中 id
是一个值标识符。要获取其中一个值,请在将 id
替换为实际 id 值后调用 r.FormValue("rating[id]")
。
我建议打印表格以查看发生了什么:
fmt.Printf("%+v\n", r.Form) // No () following Form, Form is not a function
form is an url.Values。 url.Values 是一个 map[string][]string。您可以按如下方式遍历表单:
for key, values := range r.Form { // range over map
for _, value := range values { // range over []string
fmt.Println(key, value)
}
}
对于正在寻找此问题答案的其他人,我找到了 Gorilla's schema really helpful. It allows you to parse forms to structs and has support for arrays and nested structs. When you combine that with guregu's null 包,您可以轻松地解析带有可选字段的结构。
示例:
package models
import (
"github.com/gorilla/schema"
"gopkg.in/guregu/null.v3"
)
type User struct {
Id null.Int `db:"id" json:"id"`
// Custom mapping for form input "user_name"
Name string `db:"user_name" json:"user_name" schema:"user_name"`
EmailAddress string `db:"email_address" json:"email_address"`
OptionalField null.String `db:"optional_field" json:"optional_field"`
}
示例html
<form>
<input type="text" name="user_name">
<input type="text" name="EmailAddress">
<input type="text" name="OptionalField">
</form>
如果我有以下表单设置:
{{ range $key, $value := .Scores }}
<input id="{{$value.Id}}_rating__1" type="radio" name="rating[{{$value.Id}}]" value="-1">
<input id="{{$value.Id}}_rating__0" type="radio" name="rating[{{$value.Id}}]" value="0">
<input id="{{$value.Id}}_rating__2" type="radio" name="rating[{{$value.Id}}]" value="+1">
{{ end }}
我怎样才能正确提取该数据?知道那里.Scores
可以包含多个结构
func categoryViewSubmit(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
log.Fatal(err)
}
fmt.Println("POST")
fmt.Printf("%+v\n", r.Form()) // annot call non-function r.Form (type url.Values)
fmt.Printf("%+v\n", r.FormValue("rating")) // Returns nothing
}
表单键看起来像 rating[id]
,其中 id
是一个值标识符。要获取其中一个值,请在将 id
替换为实际 id 值后调用 r.FormValue("rating[id]")
。
我建议打印表格以查看发生了什么:
fmt.Printf("%+v\n", r.Form) // No () following Form, Form is not a function
form is an url.Values。 url.Values 是一个 map[string][]string。您可以按如下方式遍历表单:
for key, values := range r.Form { // range over map
for _, value := range values { // range over []string
fmt.Println(key, value)
}
}
对于正在寻找此问题答案的其他人,我找到了 Gorilla's schema really helpful. It allows you to parse forms to structs and has support for arrays and nested structs. When you combine that with guregu's null 包,您可以轻松地解析带有可选字段的结构。
示例:
package models
import (
"github.com/gorilla/schema"
"gopkg.in/guregu/null.v3"
)
type User struct {
Id null.Int `db:"id" json:"id"`
// Custom mapping for form input "user_name"
Name string `db:"user_name" json:"user_name" schema:"user_name"`
EmailAddress string `db:"email_address" json:"email_address"`
OptionalField null.String `db:"optional_field" json:"optional_field"`
}
示例html
<form>
<input type="text" name="user_name">
<input type="text" name="EmailAddress">
<input type="text" name="OptionalField">
</form>