How to make a server in golang?

coldplay.xixi
Release: 2020-06-20 09:46:38
Original
4092 people have browsed it

How to make a server in golang?

How to make a server in golang?

How to make a server in golang:

We know that the net/http package in golang supports the network very well, which will allow us to compare It is easy to set up a relatively simple server. Let's take a look at a piece of code

func sayHi(w http.ResponseWriter, r *http.Request)  {
    fmt.Fprint(w,"Hi")
}
func main() {
    http.HandleFunc("/sayHi", sayHi)
    log.Fatal(http.ListenAndServe("localhost:8080", nil))
}
Copy after login

A relatively simple server is set up here. What does this mean? Now let’s talk about it briefly. Here we listen to a port of our local machine, then accept the client’s request, and then respond to the client with the corresponding data.

Let's look at a piece of code again

func sayHi(w http.ResponseWriter, r *http.Request)  {
    fmt.Fprint(w,"Hi")
}
func main() {
    serveMux := http.DefaultServeMux
    serveMux.HandleFunc("/sayHi", sayHi)
    log.Fatal(http.ListenAndServe("localhost:8080", serveMux))
}
Copy after login

The two pieces of code have the same function, but there are some differences in the writing method. Let's analyze what these Handler, HandlerFunc, and DefaultServeMux are used for.

In the first piece of code, let’s take a look at the source code for the function HandleFunc.

func HandleFunc(pattern string, handler func(ResponseWriter, *Request)) {
    DefaultServeMux.HandleFunc(pattern, handler)
}
Copy after login

In the source code, calling this function will call a method in it.

func (mux *ServeMux) HandleFunc(pattern string, handler func(ResponseWriter, *Request)) {
    mux.Handle(pattern, HandlerFunc(handler))
}
Copy after login

This method is defined on the ServeMux structure, so what exactly does this ServeMux do? Don't worry about it for now, let's follow the source code and look down. Here, a Handle method defined on the ServeMux structure is called. Let's see what the specific function of this method is.

func (mux *ServeMux) Handle(pattern string, handler Handler) {
    mux.mu.Lock()
    defer mux.mu.Unlock()
    if pattern == "" {
        panic("http: invalid pattern")
    }
    if handler == nil {
        panic("http: nil handler")
    }
    if _, exist := mux.m[pattern]; exist {
        panic("http: multiple registrations for " + pattern)
    }
    if mux.m == nil {
        mux.m = make(map[string]muxEntry)
    }
    mux.m[pattern] = muxEntry{h: handler, pattern: pattern}
    if pattern[0] != '/' {
        mux.hosts = true
    }
}
Copy after login

Here we see that something seems to be added to ServeMux, including patterns and handlers. What is the use of adding these things? Let’s go back and look at the second piece of code at the top.

serveMux := http.DefaultServeMux
serveMux.HandleFunc("/sayHi", sayHi)
Copy after login

The previous large section of code is preparation for these two lines of code. If it is written this way, we need to put our own defined function sayHi into serveMux. However, golang has already done it for us. A default serveMux is DefaultServeMux, then the url path/sayHi we need to access, the corresponding processing method is one-to-one correspondence in DefaultServeMux, so here we have a little understanding, does this DefaultServeMux store our access path and requirements? What about a collection of processing methods?

Yes, what is stored is the handler corresponding to the pattern, here we call it routing.

So how does this correspond to work? Here you need to look at the http.ListenAndServe("localhost:8080", serveMux) source code.

Let’s take a look at how to implement it. The source code here is relatively long. Let’s select the more important paragraphs to take a look.

func ListenAndServe(addr string, handler Handler) error {
    server := &Server{Addr: addr, Handler: handler}
    return server.ListenAndServe()
}
Copy after login

Here we will pass in the listening address and the handler to be processed. Looking further down

go c.serve(ctx)

Here, goroutine is started to serve each client request, and the final processing is done in c.serve In this method, there is one thing worth noting.

serverHandler{c.server}.ServeHTTP(w, w.req)
Copy after login

Here another ServeHTTP method is called, and then let’s see what this method handles.

In this method we see such a piece of code

if handler == nil {
        handler = DefaultServeMux
    }
...
handler.ServeHTTP(rw, req)
Copy after login

Here we see that if the handler is not passed in, then the default DefaultServeMux is used. Now we understand why in the first paragraph at the beginning In the code, we need to pass in a nil, and then handler.ServeHTTP(rw, req) calls the implementation method of the interface Handler. This way we can decide which handler a certain pattern should correspond to. We can probably understand it when we see this. What is the general principle of this server? Of course, this is a very superficial analysis, and there are many deeper things that need to be studied.

Recommended tutorial: "go language tutorial"

The above is the detailed content of How to make a server in golang?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template