Routing rule issues with Golang HTTP packets

Source: Internet
Author: User

Tag:serve   lan    rules    lang   func    bsp   mat   als   str   

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
}

Routing rule issues with Golang HTTP packets

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.