We know,golang
In-housenet/http
The package supports the network very well, which will make it easier for us to build a relatively simple server. Let's take a look at a piece of code.
func sayHi(w , r *) { (w,"Hi") } func main() { ("/sayHi", sayHi) (("localhost:8080", nil)) }
A relatively simple server has been established here. What does this mean? Now let me briefly talk about it. Here I listen to a port of our machine, then accept the client's request, and then respond to the client's corresponding data.
Let's look at another piece of code
func sayHi(w , r *) { (w,"Hi") } func main() { serveMux := ("/sayHi", sayHi) (("localhost:8080", serveMux)) }
The functions of these two pieces of code are the same, but there is a little difference in writing. Let's analyze theseHandler,HandlerFunc,DefaultServeMux
What are they used for?
In the first code,HandleFunc
Let's look at the source code of this function.
func HandleFunc(pattern string, handler func(ResponseWriter, *Request)) { (pattern, handler) }
In the source code, calling this function will be called in it.
func (mux *ServeMux) HandleFunc(pattern string, handler func(ResponseWriter, *Request)) { (pattern, HandlerFunc(handler)) }
This method is defined on the ServeMux structure, so what exactly does this ServeMux do? Let's not worry about it now. Let's follow the source code and look down. Here we call a Handle method defined on the ServeMux structure. Let's see what the specific function of this method is.
func (mux *ServeMux) Handle(pattern string, handler Handler) { () defer () if pattern == "" { panic("http: invalid pattern") } if handler == nil { panic("http: nil handler") } if _, exist := [pattern]; exist { panic("http: multiple registrations for " + pattern) } if == nil { = make(map[string]muxEntry) } [pattern] = muxEntry{h: handler, pattern: pattern} if pattern[0] != '/' { = true } }
Here we see that we are adding some things to ServeMux, which seems to have pattern and handler. What is the use of adding these things? Let's go back and take a look at the second code above.
serveMux := ("/sayHi", sayHi)
The previous large piece of code was to prepare for these two lines of code. If we follow this writing method, we need to put the function saysHi we defined in serveMux. However, golang has made a default serveMux, namely DefaultServeMux, for us. Then the url path/sayHi we need to access, and the corresponding processing method corresponds to DefaultServeMux one by one. So at this point, we have a little understanding. Is this DefaultServeMux a collection that stores our access path and the methods to be processed?
Yes, the handler corresponding to pattern is stored, which we call it the route here.
So how does this work? Here we need to take a look("localhost:8080", serveMux)
Source code.
Let's take a look at how to implement it. The source code here is relatively long. Let's choose a few more important paragraphs to take a look.
func ListenAndServe(addr string, handler Handler) error { server := &Server{Addr: addr, Handler: handler} return () }
Here you pass in the listen address and the handler to be processed, and then read it down
go (ctx)
Use startup heregoroutine
To serve every client's request, the final processing isAmong this method, there is one thing worth noting in this method.
serverHandler{}.ServeHTTP(w, )
Here we call a ServeHTTP method, and then we will see what this method handles.
In this method we see a piece of code
if handler == nil { handler = DefaultServeMux } ... (rw, req)
Here, if there is no handler passed in, then the default one will be used.DefaultServeMux
Now we understand why we have to pass in a nil in the first code at the beginning, and then(rw, req),
It's called the interfaceHandler
The implementation method can determine which handler a pattern should correspond to. After seeing this, we can roughly understand the general principle of this server. Of course, this is a very superficial analysis, and there are still many deep-seated things that need to be studied.
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.