getting checkbox values with r.FormValue

2,266 views
Skip to first unread message

will

unread,
Feb 12, 2016, 12:08:08 PM2/12/16
to golang-nuts
Dear Gophers,

I have the following scenario in a web form

<input type="checkbox" name="cars" value="mercedes" >
Mercedes<br>
<input type="checkbox" name="cars" value="bmw" >BMW<br>

In golang if i try:

var myCars []string = r.FormValue("cars"), wont work. What should i use to get the values of all the selected vehicles (assuming more than one is selected



James Bardin

unread,
Feb 12, 2016, 12:18:01 PM2/12/16
to golang-nuts


On Friday, February 12, 2016 at 12:08:08 PM UTC-5, will wrote:

var myCars []string = r.FormValue("cars"), wont work. What should i use to get the values of all the selected vehicles (assuming more than one is selected



r.ParseForm()
cars := r.Form["cars"]


> To access multiple values of the same key, call ParseForm and then inspect Request.Form directly. 

will

unread,
Feb 12, 2016, 12:27:56 PM2/12/16
to golang-nuts
I get the error cannot call non-function r.Form (type url.Values)

Shawn Milochik

unread,
Feb 12, 2016, 12:29:55 PM2/12/16
to golang-nuts
Please post some working code (as minimal as possible) duplicating the problem.

And it's r.ParseForm(), then cars := r.FormValue("cars"); I believe that was a typo in the advice above.

James Bardin

unread,
Feb 12, 2016, 1:05:33 PM2/12/16
to will, golang-nuts
On Fri, Feb 12, 2016 at 12:27 PM, will <wma...@gmail.com> wrote:
> I get the error cannot call non-function r.Form (type url.Values)

Request.Form isn't a function, is of type url.Values, which is a
map[string][]string. The FormValue method only returns the first
element of the []string.
https://golang.org/src/net/http/request.go?s=28248:28294#L933

will

unread,
Feb 12, 2016, 2:09:27 PM2/12/16
to golang-nuts, wma...@gmail.com
Hi James,

Thanks for your input.

Here is what worked:

var myCars []string
r.ParseForm()
for key, values := range r.Form { 
for _, value := range values {
if key == "cars" {
myCars = append(myCars, value)

Carlos Alberto Costa Beppler

unread,
Feb 12, 2016, 2:16:27 PM2/12/16
to golang-nuts, wma...@gmail.com
I thnk that this would work too

var myCars []string
myCars = r.Form["cars"]

James Bardin

unread,
Feb 12, 2016, 2:16:53 PM2/12/16
to will, golang-nuts
Why not simply?

myCars := r.Form["cars"]

or if myCars already contains items,

myCars = append(myCars, r.Form["cars"]...)
> --
> You received this message because you are subscribed to a topic in the
> Google Groups "golang-nuts" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/golang-nuts/sN50KliFivg/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> golang-nuts...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

will

unread,
Feb 12, 2016, 2:36:01 PM2/12/16
to golang-nuts, wma...@gmail.com
Carlos and James
You are both right.
i was trying myCars := r.Form("cars") instead of  myCars := r.Form["cars"] //Note the square brackets

I really suggest they update the documentation with such easy examples.

Thanks, it works really well

Konstantin Khomoutov

unread,
Feb 12, 2016, 2:55:27 PM2/12/16
to will, golang-nuts
On Fri, 12 Feb 2016 11:36:00 -0800 (PST)
will <wma...@gmail.com> wrote:

> Carlos and James
> You are both right.
> i was trying myCars := r.Form("cars") instead of myCars := r.Form
> ["cars"] //Note the square brackets
>
> I really suggest they update the documentation with such easy
> examples.

I'm afraid that's not realistically possible.
The docs clearly state that Form is a map[string]string,
and you're supposed to have completed at least some entry-level
tutorial on Go which would certainly contain a section on using
maps in Go. The API docs assume working knowledge of the language
itself and can't go all the way down to the basics--otherwise any
example in API docs would contain a tutorial on Go in it.
Reply all
Reply to author
Forward
0 new messages