Reputation: 1854
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? :-)
Upvotes: 0
Views: 425
Reputation: 13562
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).
Upvotes: 3