goweb build service

Keywords: Go Programming network Web Development Mobile

Introduction to web application

Web applications are everywhere in our lives. Take a look at the applications we use every day. They need to
It's either a Web application or a variation of a Web application like a mobile App. No matter which programming language, as long as
It can develop software that interacts with human beings, and it will certainly support Web application development. A brand new programming
In terms of language, the first thing its developers have to do is to build the Internet and the World
The library and framework of Wide Web interaction, and the more mature programming languages will have various kinds
Web development tools for.
Go is a budding language for people to write back-end systems simply and efficiently
Created by unification. This language has many advanced features, such as functional programming features, built-in concurrency
Programming support, modern package management systems, garbage collection features, and some all inclusive and powerful standards
Libraries, and we can also introduce third-party open source libraries if necessary.

Using the Go language for Web development is becoming increasingly popular, and many large companies are using it, such as Google
Facebook, Tencent, Baidu, Alibaba, Jingdong, Xiaomi, 360, meituan, Didi, Sina, etc

helloword

package main
import (
"fmt"
"net/http"
)
//Create processor functions
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Hello World!", r.URL.Path)
}
func main() {
http.HandleFunc("/", handler)
http.ListenAndServe(":8080", nil)
}

Creation of Web server

brief introduction

Go provides a series of standard libraries for creating Web servers, and creates a server's
The steps are very simple, as long as you call the ListenAndServe function through the net/http package and pass in the network address and negative
The handler responsible for processing the request is used as a parameter. If the network address parameter is an empty string, then
If the processor parameter is nil, the server will use the default port of 80 for network connection
The demultiplexer DefaultServeMux is recognized. Of course, we can also create
Build a multiplexer. After receiving the user's request, the multiplexer determines which one to use according to the requested URL
Processor (s) to process the request. When found, it will be redirected to the corresponding processor (s) to process the request

Use default multiplexer (DefaultServeMux)

First kind

package main
import (
"fmt"
"net/http"
)
//Create processor functions
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Processing your request through a processor function")
}
func main() {
http.HandleFunc("/", handler)
http.ListenAndServe(":8080", nil)
}

Using the processor to process requests

Second kinds

package main
import (
"fmt"
"net/http"
)
type MyHandler struct{}
func (h *MyHandler) ServeHTTP(w http.ResponseWriter, r
*http.Request) {
fmt.Fprintln(w, "Processing your request through the processor")
}
func main() {
myHandler := MyHandler{}
//Call processor
http.Handle("/", &myHandler)
http.ListenAndServe(":8080", nil)
}

Third kinds

package main
import (
"fmt"
"net/http"
"time"
)
type MyHandler struct{}
func (h *MyHandler) ServeHTTP(w http.ResponseWriter, r
*http.Request) {
fmt.Fprintln(w, "Test pass Server Structure detailed configuration service
//Device ""
}
func main() {
myHandler := MyHandler{}
//More detailed configuration of the Server through the Server structure
server := http.Server{
Addr: ":8080",
Handler: &myHandler,
ReadTimeout: 2 * time.Second,
}
server.ListenAndServe()
}

Use your own multiplexer

When creating a server, we can also create a multiplexer through the NewServeMux method

package main
import (
"fmt"
"net/http"
)
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Handle requests through a multiplexer you create")
}
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/myMux", handler)
http.ListenAndServe(":8080", mux)
}

Posted by kiju111 on Sun, 15 Dec 2019 06:27:58 -0800