Jinpu Hu
unread,Nov 8, 2011, 10:09:24 PM11/8/11Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to golang-nuts
I'm a golang newbie, I think r.FormValue method in http package should
be improved with a little change.
some relative source code:
http/request.go
if r.Form == nil {
r.Form = make(url.Values)
}
// Copy values into r.Form. TODO: make this smoother.
for k, vs := range newValues {
for _, value := range vs {
r.Form.Add(k, value)
}
}
Type of r.Form is url.Value
func (r *Request) FormValue(key string) string {
if r.Form == nil {
r.ParseMultipartForm(defaultMaxMemory)
}
if vs := r.Form[key]; len(vs) > 0 {
return vs[0]
}
return ""
}
url/url.go
Type of url.Values is map[string][]string
func (v Values) Get(key string) string {
if v == nil {
return ""
}
vs, ok := v[key]
if !ok || len(vs) == 0 {
return ""
}
return vs[0]
}
func (v Values) Add(key, value string) {
v[key] = append(v[key], value)
}
First, r.FormValue can use r.Form.Get(key) instead of more code block;
Second, like multiply checkbox with same name, xxx for example,
r.Form[xxx] will be []string{xxx_value1, xxx_value2}, i think
r.FormValue should return all values rather than fist.
maybe:
add a new methods
func (r *Request) FormValues(key string) []string {
if r.Form == nil {
r.ParseMultipartForm(defaultMaxMemory)
}
if vs := r.Form[key]; len(vs) > 0 {
return vs
}
return nil
}
or change FormValue method , use strings.Join with some special
separate.
func (r *Request) FormValue(key string) string {
if r.Form == nil {
r.ParseMultipartForm(defaultMaxMemory)
}
if vs := r.Form[key]; len(vs) > 0 {
if len(vs) == 1 {
return vs[0]
} else {
strings.Join(vs, ",") // use , sep for example
}
}
return ""
}