Go Web开发入坑指南

原创做者,公众号【程序员读书】,欢迎关注公众号,转载文章请注明出处哦。nginx

在Go语言中开发Web应用,真的是一件很是简单的事情,由于Go语言标准库中就有很是成熟且简单的Web开发包:net/http程序员

net/http封装了开发Web应用所须要的大部分功能,所以,在Go语言中使用net/http开发Web应用程序时,咱们甚至都不用像其余语言(好比PHP)同样须要本身再搭一个Apache或nginx等Web服务器,而是只须要简单几行代码就能够搭建一个Web服务应用。web

Web基础

固然,虽然使用Go的net/http包能够简单开发Web应用,但咱们在开发中仍然须要牢固地掌握开发Web程序所须要的基础知识,而Web开发中最基础和最核心的知识就是:HTTP协议编程

http协议是Web服务器与客户端(最多见就是浏览器)之间通信的语言与规范,浏览器向Web发起请求到Web服务器响应并结束链接,整个过程以下图所示:浏览器

图片摘自《HTTP权威指南》

请求与响应

一个完整http事务,由一个客户端的请求和Web服务器响应构成,客户端发起的请求,包括三个部分:请求行请求头请求体,而Web服务器的响应一样包含三部分:响应行响应头响应体,以下图所示:。bash

图片摘自《HTTP权威指南》

http协议的相关知识远不仅这些,咱们有空再谈谈。服务器

Go建立Web服务器的几种方式

http.HandleFunc函数

使用HandleFunc函数是http封装好的一个函数,能够直接使用,第一个参数是web请求路径,第二个参数是的func(writer http.ResponseWriter, request *http.Request)函数。函数

再使用http.ListenAndServe(":8080",nil)语句,监听8080端口,运行程序后。学习

使用http://localhost:8080,便会输出一块儿学习Go Web编程吧ui

其中http.ResponseWriter表明对客户端的响应体,而http.Request表明客户端发送服务器的请求数据。

func hello(writer http.ResponseWriter, request *http.Request) {
    writer.Write([]byte("一块儿学习Go Web编程吧"));
}

func main(){
    http.HandleFunc("/hello",hello)
    log.Fatal(http.ListenAndServe(":8080",nil))
}

复制代码

http.Handle函数

HandleFunc同样,Handle也是http封装好的函数,第一个参数跟HandleFunc同样,而第二个参数则是必须是实现了http.Handler接口的类型,http.Handler在http包的定义以下:

type Handler interface {
	ServeHTTP(ResponseWriter, *Request)
}
复制代码

下面咱们定义一个Controller结构体,在该结构定义ServeHTTP方法,所以Controller结构也实现http.Handler接口,而经过http.HandlerFunc也能将hello方法转成一个实现http.HandlerFunchttp.HandlerFunc也实现http.Handlerhttp.HandlerFunc在http包的定义以下:

type HandlerFunc func(ResponseWriter, *Request)

// ServeHTTP calls f(w, r).
func (f HandlerFunc) ServeHTTP(w ResponseWriter, r *Request) {
	f(w, r)
}
复制代码

其实,在上面的例子中,咱们将hello传给http.HandleFunc函数时,HandleFunc函数也是使用http.HandlerFunc将hello转换成http.HandlerFunc的。

下面有关http.Handle的示例:

type Controller struct {}
func (c Controller)ServeHTTP(writer http.ResponseWriter, request *http.Request){
    writer.Write([]byte("hello,1"));
}

func hello(writer http.ResponseWriter, request *http.Request) {
    writer.Write([]byte("hello,2"));
}

func main(){
    http.Handle("/hello1",&Controller{})
    http.Handle("/hello2",http.HandlerFunc(hello))
    log.Fatal(http.ListenAndServe(":8080",nil))
}
复制代码

运行程序后,在浏览器输入下面的地址:

http://localhost:8080/hell1, 输出:hello,1

http://localhost:8080/hell2, 输出:hello,2

http.ServeMux

不管是使用http.Handle仍是http.HandleFunc函数,其实底层代码都是使用http.DefaultServeMuxDefaultServeMux的定义以下代码所示:

var DefaultServeMux = &defaultServeMux

var defaultServeMux ServeMux
复制代码
type Controller struct {}
func (c Controller)ServeHTTP(writer http.ResponseWriter, request *http.Request){
    writer.Write([]byte("hello,1"));
}

func hello(writer http.ResponseWriter, request *http.Request) {
    writer.Write([]byte("hello,2"));
}

func main(){
    mux := &http.ServeMux{}
    mux.HandleFunc("/hello1",hello)
    mux.Handle("/hello2",http.HandlerFunc(hello))
    mux.Handle("/hello3",&Controller{})

    log.Fatal(http.ListenAndServe(":8080",mux))
}
复制代码

运行程序后,在浏览器输入下面的地址:

http://localhost:8080/hell1, 输出:hello,1

http://localhost:8080/hell2, 输出:hello,1

http://localhost:8080/hell3, 输出:hello,2

http.Server

http.Server是http包中对web更加底层的支持,咱们前面使用的方法,都是对http.Server的封装而已,若是直接使用http.Server,则能够自定义更多的参数,若是链接超时等参数,所以咱们下面直接使用http.Server开发Web服务。

func main() {
    myHandler := &http.ServeMux{}
    myHandler.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
        w.Write([]byte("hello"))
    })
    s := &http.Server{
        Addr:           ":8080",
        Handler:        myHandler,
        ReadTimeout:    10 * time.Second,
        WriteTimeout:   10 * time.Second,
        MaxHeaderBytes: 1 << 20,
    }
    log.Fatal(s.ListenAndServe())
}

复制代码

运行程序后,在浏览器输入下面的地址:

http://localhost:8080/hello, 输出:hello

总结

经过上面的例子,能够看出Go Web开发可很简单,可是实际中,一个真正Web应用所要作的事,远不仅这么简单,对于Go Web开发,仍是有不少东西要学的。


你的关注,是我写做路上最大的鼓励!

相关文章
相关标签/搜索