使用全局变量在Go Web应用程序中存储数据库句柄是一个好主意吗? [关闭]

Official guide says it's okay to use a global variable to cache templates:

http://golang.org/doc/articles/wiki/

First we create a global variable named templates, and initialize it with ParseFiles.

Can global variables be used to store DB handles and repository objects? Or do they have to be initialized for every request?

Thanks a lot

Yes, that's perfectly fine, it's used in the official Go packages all over the place, now if you gonna modify these objects from your handlers, you will have to use a mutex so you wouldn't run into races.