This is a creation in Article, where the information may have evolved or changed.
1, call the following method to open an HTTP monitoring service
http. Handlefunc ("/hello/", Hellohandler)
ERR: = http. Listenandserve (": 8080", nil)
If err= Nil {
Log. Fatal ("Listenandserve:", err.) Error ())
}
2, the route rule added "/hello" and "/hello/" two keys corresponding to the handler (Hellohandler)
3, Tracking Http\server.go Source view to the route matching rules, through the highlighted code can let us analyze it,
If we ask for route "/hello/beijing", then "/hello/" will be matched, but this is usually not what we want,
If you use native HTTP packet routing rules to avoid this situation, do not end with "/" when registering the route, i.e. register "/hello" instead of "/hello/"
Func Pathmatch (pattern, path string) bool {
If Len (pattern) = = 0 {
Should not happen
return False
}
N: = Len (pattern)
pattern[n-1]! = '/' {
return pattern = = Path
}
Return len (path) >= n && path[0:n] = = Pattern
}