dongxuan1660 2015-02-01 03:15 采纳率: 100%
浏览 11
已采纳

Golang解析形式

If I have the following form setup:

{{ 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 }}

How can I then extract that data correctly? Knowing that there .Scores can contain multiple structs

func categoryViewSubmit(w http.ResponseWriter, r *http.Request) {
    err := r.ParseForm()
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println("POST")

    fmt.Printf("%+v
", r.Form()) // annot call non-function r.Form (type url.Values)
    fmt.Printf("%+v
", r.FormValue("rating")) // Returns nothing
}
  • 写回答

2条回答 默认 最新

  • dpe77294 2015-02-01 04:53
    关注

    The form keys look like rating[id] where id is a value identifier. To get one of the values, call r.FormValue("rating[id]") after substituting id for an actual id value.

    I suggest printing the form to see what's going on:

    fmt.Printf("%+v
    ", r.Form)  // No () following Form, Form is not a function
    

    The form is an url.Values. An url.Values is a map[string][]string. You can iterate through the form as follows:

    for key, values := range r.Form {   // range over map
      for _, value := range values {    // range over []string
         fmt.Println(key, value)
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 蓝桥oj3931,请问我错在哪里
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染