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).

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

报告相同问题?

悬赏问题

  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法