The URL I am receiving looks like this:
/controller/action?columns[0][data]=foo&columns[1][data]=bar&columns[2][data]=bar&columns[3][data]=bar&columns[4][data]=bar
So I have an array of objects. How can I parse this into a slice of structs?
What I have in my controller action is this:
func (this *MyController) Foo() {
type column struct{
Data string
}
columns := []column{}
if err := this.Ctx.Input.Bind(&columns, "columns"); err != nil {
log.Fatal(err)
} else {
log.Println("OK, got:", columns)
}
}
And the output of this is:
OK, got: [{} {} {} {} {} {}]
Bind successfully detects there are five columns but fails to map the data
field. I experimented with this and believe there are two problems:
The bind seems to only support the exact syntax shown in the documentation:
?id=123&isok=true&ft=1.2&ol[0]=1&ol[1]=2&ul[]=str&ul[]=array&user.Name=astaxie
user.Name=astaxie
works while user[Name]=astaxie
does not.
The 2nd problem is, the lower case of data
. My experiments have shown user.Name
works while user.name
does not.
So the data bind method expects me to get the parameters in the form columns[0].Data=foo
while I have columns[0][data]=foo
The first one indeed works. But I have no control over the URL, I have to accept it as it is, with lower case data
in square brackets. The request is generated by DataTables.