I have a specific struct that contains some url parameters, I want to build a url parameters string using reflect to iterate through the struct field, so that I wont care about what's the struct really contains.
Let's say I have a struct like this:
type Student struct {
Name string `paramName: "username"`
Age int `paramName: userage`
}
And I assign a student like this:
s := Student{
Name : "Bob",
Age : 15,
}
I want to build a query parameter string like this for this student instance:
username=Bob&userage=15
So far I have got:
func (s Student) buildParams() string {
st := reflect.TypeOf(s)
fieldCount := st.NumField()
params := ""
for i := fieldCount; i > 0 ; i-- {
params = params + "&" + st.Field(i).Tag.Get("paramName") + "=" + st.Field(i).Name
}
return params
}
But s.buildParams()
gets me nothing, not event the tag value of paramName in each field :-(
So How can I do this?
Aucun commentaire:
Enregistrer un commentaire