This is a creation in Article, where the information may have evolved or changed.
Original link
Go (golang.org) is a system language that provides the HTTP protocol support in the standard library, enabling him to develop a Web server quickly and easily. At the same time, the go language offers developers a lot of convenience. In this blog post we will list the ways to develop an HTTP server using go, and then analyze how these different methods work and why they work.
Before you begin, assume that you already know some basic syntax for go, understand the principle of HTTP, and know what a Web server is. Then we can start the HTTP server version of the famous "Hello World".
It's better to see the results first and then explain the details. Create a http1.go
file that is called. Then copy the following code into the past:
Package Mainimport ("io" "net/http") Func Hello (w http. Responsewriter, R*http. Request) {io. WriteString (W,"Hello world!")}func Main () {http. Handlefunc ("/", hello) http. Listenandserve (": 8000", nil)}
The terminal executes go run http1.go
, and then the browser accesses the http://localhost:8000. You will see the Hello world!
display on the screen.
Why is that? All the running packages in the Go language must be named main
. We created the main and hello two functions.
In the main function, we call a function from the net/http
package http.HandleFucn
to register a handler function, in this case the Hello function. This function accepts two parameters. The first one is a string, which will be routed, in this case the root route. The second function is a func (ResponseWriter, Request)
signature. As you can see, our hello function is such a signature. In the next line http.ListenAndServe(":8000", nil)
, the 8000 port that listens for localhost, temporarily ignores nil.
In the Hello function we have two parameters, one is http.ResponseWriter
type. It resembles a response stream and is actually an interface type. The second is a http.Request
type, similar to an HTTP request. We don't have to use all the arguments, just like in the Hello function. If we want to return to "Hello World", then we only need to use HTTP. Responsewriter,io. WriteString, is a helper function that will want to write data to the output stream.
Here is a slightly more complex example:
Package Mainimport ("io" "net/http") Func Hello (w http. Responsewriter, R*http. Request) {io. WriteString (W,"Hello world!")}func Main () {mux:=http. Newservemux () Mux. Handlefunc ("/", hello) http. Listenandserve (": 8000", MUX)}
In the above example, we are not using in the function http.ListenAndServe
nil
. It *ServeMux
has been replaced. You might guess this example is like the example I have above. Registering the Hanlder function mode with HTTP is the Servemux.
The following is a more complex example:
Import ("io" "net/http") Func Hello (w http. Responsewriter, R*http. Request) {io. WriteString (W,"Hello world!")}varMUX map[string]func (http. Responsewriter, *http. Request) Func main () {server:=http. server{Addr:": 8000", Handler:&myhandler{},} Mux= Make (map[string]func (http. Responsewriter, *http. Request)) mux["/"] =Hello server. Listenandserve ()}type MyHandlerstruct{}func (*myhandler) servehttp (w http. Responsewriter, R *http. Request) {ifH, OK: =Mux[r.url. String ()]; OK {h (W, R)return} io. WriteString (W,"My Server:"+R.url. String ())}
To verify your conjecture, we have done the same thing, that is, to output Hello World on the screen again. Now, however, we don't define SERVEMUX, but we use HTTP. Server. So you can see why I was running the server with the Net/http package.