goweb- build a service

Introduction to web applications

Web application in our lives everywhere. Look at the various applications we use every day, they are to
what is a Web application, either a variant of such Web App mobile applications. No matter what kind of programming language, as long as
it can develop software and human interaction, it is bound to support Web application development. On a new programming
language, its developers first need to do a thing, is to build the Internet (Internet) and the World Wide Web (World
Wide Web) interaction library (library) and the frame, and those more mature programming languages there will be a myriad
of Web development tools.
Go is a language just beginning to emerge, it is to allow people to easily and efficiently write back-end system
system created. This language has many advanced features, characteristics such as functional programming aspects of the built-in concurrent
programming support, modern package management system, garbage collection features, as well as some all-encompassing powerful standard
library, and if required we can also the introduction of third party open source libraries.

Using the Go language Web development is becoming increasingly popular, many large companies are using, such as Google,
Facebook, Tencent, Baidu, Alibaba, Jingdong, millet and 360, the US group, drops and Sina

helloword

package main
import (
"fmt"
"net/http"
)
//创建处理器函数
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)
}

Create a Web server

Brief introduction

Go offers a range of standard library for creating Web server and create a server Go through
the steps as simple as calling ListenAndServe function by net / http incoming network packets and addresses as well as negative
processing requests responsibilities processor (handler) as a parameter on it. If the network address parameter is an empty string, then
it server uses the default network connection port 80; if the processor parameter to nil, the server uses the default
recognize multiplexer DefaultServeMux, of course, we can call the function NewServeMux create
build a multiplexer. After the multiplexer receives the user's request according to the URL request to determine which of the
processors to process the request, after finding redirected to the corresponding processor to process the request.

Default multiplexer (DefaultServeMux)

The first

package main
import (
"fmt"
"net/http"
)
//创建处理器函数
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "正在通过处理器函数处理你的请求")
}
func main() {
http.HandleFunc("/", handler)
http.ListenAndServe(":8080", nil)
}

Using a processor to process the request

The second

package main
import (
"fmt"
"net/http"
)
type MyHandler struct{}
func (h *MyHandler) ServeHTTP(w http.ResponseWriter, r
*http.Request) {
fmt.Fprintln(w, "正在通过处理器处理你的请求")
}
func main() {
myHandler := MyHandler{}
//调用处理器
http.Handle("/", &myHandler)
http.ListenAndServe(":8080", nil)
}

The third

package main
import (
"fmt"
"net/http"
"time"
)
type MyHandler struct{}
func (h *MyHandler) ServeHTTP(w http.ResponseWriter, r
*http.Request) {
fmt.Fprintln(w, "测试通过 Server 结构详细配置服务
器")
}
func main() {
myHandler := MyHandler{}
//通过 Server 结构对服务器进行更详细的配置
server := http.Server{
Addr: ":8080",
Handler: &myHandler,
ReadTimeout: 2 * time.Second,
}
server.ListenAndServe()
}

Create your own using a multiplexer

When you create a server, we can also create a multiplexer method by NewServeMux

package main
import (
"fmt"
"net/http"
)
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "通过自己创建的多路复用器来处理请求")
}
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/myMux", handler)
http.ListenAndServe(":8080", mux)
}

Guess you like

Origin www.cnblogs.com/ygjzs/p/12045927.html