Go HTTP source parsing (i)

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Go Web Tour

This starts with the Go Web tour, where I divide the journey into three sub-journeys:

    1. SOURCE parsing
    2. Framework Interpretation
    3. Middleware usage

So in this journey we will appreciate the magnificent source code, the frame of the odd-yan, the scenery of the middleware. In the following time I will follow the above list in turn.
Now embark on the journey to the Go web.

func firstHandler(w http.ResponseWriter, r *http.Request) {     io.WriteString"Hello,world")}func main() {        http.HandleFunc("/hello", firstHandler)    http.ListenAndServe(":8080", nil)}

In the main function, HTTP. Handlefunc sets all the handler functions for/hello requests to the path to Firsthandler.
Next, call HTTP. Listenandserve, start listening on port 8080 will block until it exits.

We will use source parsing as the opening of the entire journey.
The go language, as the C language in the Internet, makes web development simple and concise. First of all, let's introduce one of the most important packages (package) that is involved in making web development easy (usually directly in the package when encountering packages) HTTP.

package http在service.go中。// HTTP server.  See RFC 2616.package http//声明一个Handler接口,若某函数实现了ServeHTTP函数就实现了该接口typeinterface {    ServeHTTP(ResponseWriter, *Request)}

So how do we implement this interface?
In fact, in Service.go, he has implemented the function by default.

typeServemuxstruct{//define Routing rulesMu sync. Rwmutex//lock mechanism, because concurrent processing requires a lockMMap[string]muxentry//routing, using map structureHostsBOOL //Whether any patterns contain hostnames}typeMuxentrystruct{//RoutingExplicitBOOL   //Whether the exact matchH Handlerwhen the route is matched, the selected processingPatternstring //Match string}//Servehttp dispatches the request to the handler whose//Pattern most closely matches the request URL.//Routing structure Servemux implement Servehttp functionfunc(Mux *servemux) Servehttp (w responsewriter, R *request) {ifR.requesturi = ="*"{ifR.protoatleast(1,1) {W.header (). Set ("Connection","Close")} w.writeheader (Statusbadrequest)return} h, _: = Mux. Handler (R) H.servehttp (W, R)}

From the GODOC hint, you know that when a request comes in, a route match is made, and the process is done by matching a string in servemux.m.
After the match is successful, the corresponding handler of MuxEntry.h is selected for processing, and this step is called servehttp implementation.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Related Article

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.