首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >Go语言单控制器和多控制器

Go语言单控制器和多控制器

原创
作者头像
IT工作者
发布2022-03-30 23:22:25
发布2022-03-30 23:22:25
7390
举报
文章被收录于专栏:程序技术知识程序技术知识

一. 单控制器

在Golang的net/http包下有ServeMux实现了Front设计模式的Front窗口,ServeMux负责接收请求并把请求分发给处理器(Handler)

http.ServeMux实现了Handler接口

代码语言:javascript
复制
type Handler interface {
    ServeHTTP(ResponseWriter, *Request)
}
type ServeMux struct {
    mu    sync.RWMutex
    m     map[string]muxEntry
    hosts bool // whether any patterns contain hostnames
}
func (mux *ServeMux) ServeHTTP(w ResponseWriter, r *Request) {
    if r.RequestURI == "*" {
        if r.ProtoAtLeast(1, 1) {
            w.Header().Set("Connection", "close")
        }
        w.WriteHeader(StatusBadRequest)
        return
    }
    h, _ := mux.Handler(r)
    h.ServeHTTP(w, r)
}

自定义结构体,实现Handler接口后,这个结构体就属于一个处理器,可以处理全部请求

无论在浏览器中输入的资源地址是什么,都可以访问ServeHTTP

代码语言:javascript
复制
package main

import "fmt"
import "net/http"

type MyHandler struct {
}

func (mh *MyHandler) ServeHTTP(res http.ResponseWriter, req *http.Request) {
   fmt.Fprintln(res,"输出内容")
}

func main() {
   myhandler := MyHandler{}
   server := http.Server{
      Addr:    "127.0.0.1:8090",
      Handler: &myhandler,
   }
   server.ListenAndServe()
}

二.多控制器

在实际开发中大部分情况是不应该只有一个控制器的,不同的请求应该交给不同的处理单元.在Golang中支持两种多处理方式

多个处理器(Handler)

多个处理函数(HandleFunc)

使用多处理器

使用http.Handle把不同的URL绑定到不同的处理器

在浏览器中输入http://localhost:8090/myhandler或http://localhost:8090/myother可以访问两个处理器方法.但是其他URl会出现404(资源未找到)页面

代码语言:javascript
复制
package main

import "fmt"
import "net/http"

type MyHandler struct{}
type MyOtherHandler struct{}

func (mh *MyHandler) ServeHTTP(res http.ResponseWriter, req *http.Request) {
   fmt.Fprintln(res, "第一个")
}
func (mh *MyOtherHandler) ServeHTTP(res http.ResponseWriter, req *http.Request) {
   fmt.Fprintln(res, "第二个")
}

func main() {
   myhandler := MyHandler{}
   myother := MyOtherHandler{}
   server := http.Server{
      Addr: "localhost:8090",
   }
   http.Handle("/myhandler", &myhandler)
   http.Handle("/myother", &myother)
   server.ListenAndServe()
}

多函数方式要比多处理器方式简便.直接把资源路径与函数绑定

代码语言:javascript
复制
package main

import "fmt"
import "net/http"

//不需要定义结构体
//函数的参数需要按照ServeHTTP函数参数列表进行定义
func first(res http.ResponseWriter, req *http.Request) {
   fmt.Fprintln(res, "第一个")
}
func second(res http.ResponseWriter, req *http.Request) {
   fmt.Fprintln(res, "第二个")
}

func main() {
   server := http.Server{
      Addr: "localhost:8090",
   }
   //注意此处使用HandleFunc函数
   http.HandleFunc("/first", first)
   http.HandleFunc("/second", second)
   server.ListenAndServe()
}

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档