Golang “Best practice” to handle connection to database

半城伤御伤魂 提交于 2019-12-07 04:20:40

问题


I have a webservice written in Go and currently I have this Global package I import everywhere which contains the connection to MongoDB (via MGO), but I have to say that this feels very icky to me. What is the best practice of maintaining connections to datasources in Go? I come from a PHP world, hence the Global :S


回答1:


There is nothing wrong with importing a package which exports an initialized *mgo.Session object either as a package-level variable or through an accessor/initializer. Maybe the latter appeals better to classical "OOP-ists".



来源:https://stackoverflow.com/questions/23333753/golang-best-practice-to-handle-connection-to-database

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!