在Go语言中解析表单数组

huangapple go评论157阅读模式
英文:

Parsing form arrays In Go

问题

我需要能够在我的HTML表单中做类似这样的事情:

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

问题是,如果我打印出r.Form(当然,在执行了r.ParseForm()之后),我发现Go将其解析为单独的字符串,而不是我想要的嵌套层次结构。

我在http://www.gorillatoolkit.org/pkg/schema中找到了一个替代方案,但我非常想知道是否有一种本地/原生的解决方案,因为通常我倾向于尽量减少使用外部包,除非我确实需要它们。

更具体地说,在上面的示例中,当我执行r.Form["cart"]时,我希望得到整个嵌套的切片/映射,但显然这不是它的工作方式。我得到的值是[]。

使用schema是解决这个问题的最佳方案吗?还是有可能实现我想要的效果?如果可以的话,你会如何做? 在Go语言中解析表单数组

英文:

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

&lt;form method=&quot;POST&quot;&gt;
    &lt;input type=&quot;text&quot; name=&quot;cart[items][1][qty]&quot; value=&quot;3&quot;/&gt;
    &lt;input type=&quot;text&quot; name=&quot;cart[items][2][qty]&quot; value=&quot;7&quot;/&gt;
    &lt;input type=&quot;submit&quot; value=&quot;Update cart&quot;/&gt;
&lt;/form&gt;

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[&quot;cart&quot;], 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? 在Go语言中解析表单数组

答案1

得分: 3

这个语法实际上并不是法定标准。它有时在网站后端由PHP驱动时使用,但并不常见。

我认为标准的Go库没有理由支持它。

从某种意义上说,存在一种本地解决方案,你可以使用字符串操作等工具来实现它。然而,现在实现它没有意义,因为你总是可以发送特定任务的JSON数据。

即使你坚持要创建它,也不会像在PHP中那样简单,因为Go是一种类型化的语言,而PHP不是。访问这样的结构也不容易,你将不得不将其实现为map[string]interface{},这不是Go的正确方式(顺便说一下,Go的正确方式是使用类型化的数据结构)。

英文:

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

huangapple
  • 本文由 发表于 2015年8月1日 19:18:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/31761219.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定