Get checked checkbox values (and radio values)

I am trying to fetch form data and create an email. When dealing with a set of checkboxes the normal r.FormValue("name") as I only want the checked values.

This code iterates over all form elements (radio, checkboxes, text, email etc):

for key, values := range r.Form { // range over map
	for _, value := range values { // range over []string
		fmt.Println(key, value)
	}
}

But I want to only get the “checked” checkboxes. Nothing else. This code works, but is somewhat clumsy IMO:

for key, values := range r.Form { // range over map
	for _, value := range values { // range over []string
		if key == "addon" {
			fmt.Println(key, value)
		}
	}
}

What I am searching for is (pseudocode) or even simpler code:

for key, values := range r.Form == "addon" { range over addon checkboxes)
	for _, value := range values 
		fmt.Println(key, value)
	}
}

Is there any better way to get the values of the checked checkboxes?

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.