duanheyi7147 2015-08-01 11:18
浏览 113
已采纳

在Go中解析表单数组

I need to be able to to something like this in my HTML forms:

<form method="POST">
    <input type="text" name="cart[items][1][qty]" value="3"/>
    <input type="text" name="cart[items][2][qty]" value="7"/>
    <input type="submit" value="Update cart"/>
</form>

Problem is, if I print out r.Form (after r.ParseForm() of course!), it is revealed that Go parses it as separate strings - not the nested hierarchy I'm after.

I was able to find an alternative in http://www.gorillatoolkit.org/pkg/schema , however I'd be very interested in finding out if there's a native/vanilla solution, since I usually tend to try an limit external packages, if I don't need them.

To be more specific, in the above example I'd expect to get the entire nested slice/map, when I do r.Form["cart"], but that's obviously not how it works. The value I get is [].

Is using schema the best solution for this, or is it possible to achieve exactly what I'm after? If then, how would you do it? :-)

  • 写回答

1条回答 默认 最新

  • doukun8670 2015-08-02 00:12
    关注

    This syntax is actually is not standard de jure. It is used sometimes when the site backend is powered by PHP, but not very frequently.

    I think that there is no reason for the standard Go library to support it.

    Native solution exists in a sense that you have all the tools to implement it: string manipulations, etc. However, nowadays there is no point to implement it as you always can send task-specific JSON.

    And even you insist on creating it, it will be not as simple as it is in PHP because Go is typed language and PHP is not. And accessing such structure will be not easy as you will have to implement it as map[string]interface{}, which is not the proper Go way (the Go way, BTW, is to typed data structures).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历