栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

将上下文传递给接口方法

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

将上下文传递给接口方法

在与#go-nuts上的几个有用的Gophers进行了讨论之后,根据我的判断,上述方法是“尽其所能”。

  • 使用此方法的“骗局”是,我们 两次 将对上下文结构的引用传递给我们:一次作为我们方法中的指针接收器,再一次作为结构成员,因此
    ServeHTTP
    也可以对其进行访问。
  • “ pro”是我们可以扩展结构类型以接受请求上下文结构(如gocraft / web那样)。

请注意,我们不能定义我们的处理程序上的方法

appHandler
,即
func (ah *appHandler)IndexHandler(...)
因为我们需要调用的处理程序
ServeHTTP
(即
ah.h(w,r)
)。

type appContext struct {    db        *sqlx.DB    store     *sessions.cookieStore    templates map[string]*template.Template}type appHandler struct {    handler func(w http.ResponseWriter, r *http.Request) (int, error)    *appContext // Embedded so we can just call app.db or app.store in our handlers.}// In main() ...context := &appContext{db: nil, store: nil}r.Get("/", appHandler{context.IndexHandler, context}) ...

最重要的是,它也与完全兼容,

http.Handler
因此我们仍然可以使用通用中间件来包装处理程序结构,如下所示:
gzipHandler(appHandler{context.IndexHandler,context})

(不过,我仍然愿意接受其他建议!)


更新资料

多亏了Reddit的这一出色答复,我才能够找到一个更好的解决方案,该解决方案不需要为

context
每个请求将两个引用传递给我的实例。

相反,我们仅创建一个接受嵌入式上下文和我们的处理程序类型的结构,并且

http.Handler
由于,我们仍然满足该接口
ServeHTTP
。处理程序不再是我们
appContext
类型的方法,而只是将其作为参数接受,这导致函数签名稍长,但仍然“显而易见”且易于阅读。如果我们担心“类型化”,那么我们将达到收支平衡,因为我们不再需要担心方法接收者。

type appContext struct {    db    *sqlx.DB    store *sessions.cookieStore    templates map[string]*template.Templatetype appHandler struct {    *appContext    h func(a *appContext, w http.ResponseWriter, r *http.Request) (int, error)}func (ah appHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {    // We can now access our context in here.    status, err := ah.h(ah.appContext, w, r)    log.Printf("Hello! DB: %v", ah.db)    if err != nil {        log.Printf("HTTP %d: %q", status, err)        switch status {        case http.StatusNotFound: // err := ah.renderTemplate(w, "http_404.tmpl", nil) http.NotFound(w, r)        case http.StatusInternalServerError: // err := ah.renderTemplate(w, "http_500.tmpl", nil) http.Error(w, http.StatusText(status), status)        default: // err := ah.renderTemplate(w, "http_error.tmpl", nil) http.Error(w, http.StatusText(status), status)        }    }}func main() {    context := &appContext{        db:    nil,        store: nil,        templates: nil,    }    r := web.New()    // We pass a reference to context *once* per request, and it looks simpler    r.Get("/", appHandler{context, IndexHandler})    graceful.ListenAndServe(":8000", r)}func IndexHandler(a *appContext, w http.ResponseWriter, r *http.Request) (int, error) {    fmt.Fprintf(w, "db is %q and store is %qn", a.db, a.store)    return 200, nil}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/409640.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号