From bc2756916cfcac2899f7d9df213136a0a8f18f45 Mon Sep 17 00:00:00 2001 From: Anchor Date: Sat, 17 Jan 2015 20:14:01 -0800 Subject: [PATCH] Fix section "default routing implementation" in section 13.2.md --- en/13.2.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/en/13.2.md b/en/13.2.md index afe5cec4..3cb0f498 100644 --- a/en/13.2.md +++ b/en/13.2.md @@ -9,9 +9,9 @@ The HTTP routing component is responsible for mapping HTTP requests to a corresp The router then forwards the request to the handler function (controller layer) that has been registered with that particular HTTP method and path. -## Default route to achieve +## Default routing implementation -Have been introduced in section 3.4 Go's http package Detailed, which introduced the Go's http package how to design and implement routing, here continue to be an example to illustrate: +In section 3.4, we introduced Go's `http` package in detail, which included how to design and implement routing. Here, we take another look at an example that illustrates the routing process: func fooHandler(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path)) @@ -25,14 +25,14 @@ Have been introduced in section 3.4 Go's http package Detailed, which introduced log.Fatal(http.ListenAndServe(":8080", nil)) -The above example calls the http default DefaultServeMux to add a route, you need to provide two parameters, the first parameter is the resource you want users to access the URL path( stored in r.URL.Path), the second argument is about to be executed function to provide the user access to resources. Routing focused mainly on two ideas: +The example above calls `http`'s default mux called `DefaultServeMux`, implicitly specified by the `nil` parameter in the call to `http.ListenAndServe`. The `http.Handle` function takes two parameters: the first parameter is the resource you want users to access, specified by its URL path (which is stored in `r.URL.Path`) and the second argument binds a handler function with this path. The Router has two main jobs: -- Add routing information -- According to the user request is forwarded to the function to be performed +- To add and store routing information +- To forward requests to a handler function for processing -Go add default route is through a function `http.Handle` and `http.HandleFunc`, etc. to add, the bottom is called `DefaultServeMux.Handle(pattern string, handler Handler)`, this function will set the routing information is stored in a map information in `map [string] muxEntry`, which would address the above said first point. +By default, Go routes are handled with `http.Handle` and `http.HandleFunc` types, registered by default through the underlying `DefaultServeMux.Handle(pattern string, handler Handler)` function. This function maps resource paths to handlers and stores them in a `map[string]muxEntry` map. This is the first job that we mentioned above. -Go listening port, and then receives the tcp connection thrown Handler to process, the above example is the default nil `http.DefaultServeMux`, `DefaultServeMux.ServeHTTP` by the scheduling function, the previously stored map traverse route information, and the user URL accessed matching to check the corresponding registered handler, so to achieve the above mentioned second point. +When the application is running, the Go server listens to a port. When it receives a tcp connection, it uses a `Handler` to process it. As aforementioned, since the `Handler` in the example above is `nil`, the default router `http.DefaultServeMux` is used. Using the map of previously stored routes, `DefaultServeMux.ServeHTTP` will dispatch the request to the first handler with a matching path. This is the router's second job. for k, v := range mux.m { if !pathMatch(k, path) {