- Building Microservices with Go
- Nic Jackson
- 416字
- 2021-07-15 17:28:03
Paths
We already explained how ServeMux is responsible for routing inbound requests to the registered handlers, however the way that the routes are matched can be quite confusing. The ServeMux handler has a very simple routing model it does not support wildcards or regular expressions, with ServeMux you must be explicit about the registered paths.
You can register both fixed rooted paths, such as /images/cat.jpg, or rooted subtrees such as /images/. The trailing slash in the rooted subtree is important as any request that starts with /images/, for example /images/happy_cat.jpg, would be routed to the handler associated with /images/.
If we register a path /images/ to the handler foo, and the user makes a request to our service at /images (note no trailing slash), then ServerMux will forward the request to the /images/ handler, appending a trailing slash.
If we also register the path /images (note no trailing slash) to the handler bar and the user requests /images then this request will be directed to bar; however, /images/ or /images/cat.jpg will be directed to foo:
http.Handle("/images/", newFooHandler())
http.Handle("/images/persian/", newBarHandler())
http.Handle("/images", newBuzzHandler())
/images => Buzz
/images/ => Foo
/images/cat => Foo
/images/cat.jpg => Foo
/images/persian/cat.jpg => Bar
Longer paths will always take precedence over shorter ones so it is possible to have an explicit route that points to a different handler to a catch all route.
We can also specify the hostname, we could register a path such as search.google.com/ and /ServerMux would forward any requests to http://search.google.com and http://www.google.com to their respective handlers.
If you are used to a framework based application development approach such as using Ruby on Rails or ExpressJS you may find this router incredibly simple and it is, remember that we are not using a framework but the standard packages of Go, the intention is always to provide a basis that can be built upon. In very simple cases the ServeMux approach more than good enough and in fact I personally don't use anything else. Everyone's needs are different however and the beauty and simplicity of the standard packages makes it incredibly simple to build your own route as all is needed is an object which implements the Handler interface. A quick trawl through google will surface some very good third party routers but my recommendation for you is to learn the limitations of ServeMux first before deciding to choose a third-party package it will greatly help with your decision process as you will know the problem you are trying to solve.
- Interactive Data Visualization with Python
- PHP從入門到精通(第4版)(軟件開發視頻大講堂)
- Visual Foxpro 9.0數據庫程序設計教程
- LabVIEW虛擬儀器入門與測控應用100例
- JavaScript動態網頁編程
- OpenCV 3 Blueprints
- C++程序設計教程(第2版)
- Unity 5.X從入門到精通
- MongoDB Cookbook(Second Edition)
- Android初級應用開發
- ASP.NET Core 2 High Performance(Second Edition)
- Mastering Magento Theme Design
- Server Side development with Node.js and Koa.js Quick Start Guide
- 數據分析從入門到進階
- Test-Driven iOS Development with Swift 4(Third Edition)