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)) }
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)) }
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) }
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)) }
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 } }
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)
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() }
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)
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)
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Reading and writing files safely in Go is crucial. Guidelines include: Checking file permissions Closing files using defer Validating file paths Using context timeouts Following these guidelines ensures the security of your data and the robustness of your application.

How to configure connection pooling for Go database connections? Use the DB type in the database/sql package to create a database connection; set MaxOpenConns to control the maximum number of concurrent connections; set MaxIdleConns to set the maximum number of idle connections; set ConnMaxLifetime to control the maximum life cycle of the connection.

According to news from this website on July 23, ASUS has launched a variety of server and workstation-level products powered by AMD EPYC 4004 series processors. Note from this site: AMD launched the AM5 platform and Zen4 architecture EPYC 4004 series processors in May, offering up to 16-core 3DV-Cache specifications. ASUSProER100AB6 server ASUSProER100AB6 is a 1U rack server product equipped with EPYC Xiaolong 4004 series processor, suitable for the needs of IDC and small and medium-sized enterprises. ASUSExpertCenterProET500AB6 workstation ASUSExpertCenterProET500AB6 is a

The difference between the GoLang framework and the Go framework is reflected in the internal architecture and external features. The GoLang framework is based on the Go standard library and extends its functionality, while the Go framework consists of independent libraries to achieve specific purposes. The GoLang framework is more flexible and the Go framework is easier to use. The GoLang framework has a slight advantage in performance, and the Go framework is more scalable. Case: gin-gonic (Go framework) is used to build REST API, while Echo (GoLang framework) is used to build web applications.

JSON data can be saved into a MySQL database by using the gjson library or the json.Unmarshal function. The gjson library provides convenience methods to parse JSON fields, and the json.Unmarshal function requires a target type pointer to unmarshal JSON data. Both methods require preparing SQL statements and performing insert operations to persist the data into the database.

The FindStringSubmatch function finds the first substring matched by a regular expression: the function returns a slice containing the matching substring, with the first element being the entire matched string and subsequent elements being individual substrings. Code example: regexp.FindStringSubmatch(text,pattern) returns a slice of matching substrings. Practical case: It can be used to match the domain name in the email address, for example: email:="user@example.com", pattern:=@([^\s]+)$ to get the domain name match[1].

Backend learning path: The exploration journey from front-end to back-end As a back-end beginner who transforms from front-end development, you already have the foundation of nodejs,...

Using predefined time zones in Go includes the following steps: Import the "time" package. Load a specific time zone through the LoadLocation function. Use the loaded time zone in operations such as creating Time objects, parsing time strings, and performing date and time conversions. Compare dates using different time zones to illustrate the application of the predefined time zone feature.
