Skip to content

Instantly share code, notes, and snippets.

@hevela
hevela / struct_to _URL_Values.go
Created December 28, 2022 19:57
take a struct as input and generate a url.Values object suitable for form-encoded requests
// StructToURLValues converts a struct into url.Values using the field tag (tagname param) value as the encoded field name.
func StructToURLValues(i interface{}, tagname string) url.Values {
values := url.Values{}
if i == nil {
return values
}
iVal := reflect.ValueOf(i).Elem()
buildURLValues(iVal, values, tagname)
return values