golang http

2019-07-29  本文已影响0人  copyLeft

golang http

http 挂载方法

type MyHandler string

func (m MyHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
  w.Write([]byte(m))
}

func main() {

  var msg MyHandler = "show me"
  http.Handle("/", msg)

  http.ListenAndServe(":8080", nil)
}

Server 服务器挂载方法

Server 服务器属性

ServeMux 路由挂载函数

ServeMux and Server 简单例子

router := http.NewServeMux()
    server := http.Server{}

    router.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {

        w.Write([]byte("home page"))

    })

    server.Addr = ":8080"
    server.Handler = router

    server.ListenAndServe()

Client 客户端挂载方法

快捷Handler

上一篇下一篇

猜你喜欢

热点阅读