Redigo Redis Pool是否应该是一个全球变量?

编程入门 行业动态 更新时间:2024-10-20 13:50:33
本文介绍了Redigo Redis Pool是否应该是一个全球变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在此示例中, Redigo Doc for Pool redis池是在func main中设置为全局变量。这是一个犹太人的方式来做事吗?如果你真的要左右使用全局变量,还是有更好的方法来完成同样的事情? 我见过唯一的其他解决方案,例如在将上下文传递给接口方法中:

创建一个接受嵌入式上下文和我们的处理函数的 struct type,并且感谢 ServeHTTP 。我们仍然满足 http.Handler 接口。

在你的情况下, struct 会包含 pool code>处理函数函数。 $ $ p $ type appContext struct { 池池 类型appHandler结构{ * appContext h func(a * appContext,w http.ResponseWriter,r * http.Request) (int,error)} func(ah appHandler)ServeHTTP(w http.Respons eWriter,r * http.Request){ ... } func main(){ context:=& appContext { pool:..., //任何其他数据} }

In the example here Redigo Docs for Pool the redis pool is set as a global variable in func main. Is that a kosher way to do things? Should you really be using global varibales left and right or is there a better, more preferred way of accomplishing the same thing?

解决方案

The only other solution have I seen, for instance in "Passing Context to Interface Methods" is:

create a struct that accepts an embedded context and our handler type, and we still satisfy the http.Handler interface thanks to ServeHTTP.

In your case, the struct would include the pool, and the handler function.

type appContext struct { pool Pool } type 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) { ... } func main() { context := &appContext{ pool: ..., // any other data } }

更多推荐

Redigo Redis Pool是否应该是一个全球变量?

本文发布于:2023-11-15 21:26:22,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:是一个   变量   全球   Redigo   Redis

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!