I'm reading the source code about http module, and following code confused me:
func (mux *ServeMux) shouldRedirectRLocked(host, path string) bool {
p := []string{path, host + path}
for _, c := range p {
if _, exist := mux.m[c]; exist {
return false
}
}
n := len(path)
if n == 0 {
return false
}
for _, c := range p {
if _, exist := mux.m[c+"/"]; exist {
return path[n-1] != '/' <<- why not return true directly
}
}
return false
}
As the comment above this method: shouldRedirectRLocked reports whether the given path and host should be redirected to path+"/". This should happen if a handler is registered for path+"/" but not path -- see comments at ServeMux.
c+"/" already registered, and c didn't , so I think it should return true directly, but why we have to check path[n - 1] != '/'? Is this a question about the http protocol?