如何处理未知变量或如何处理多个数据库

huangapple go评论116阅读模式
英文:

How to deal with unknown variables or How to deal with multiple databases

问题

我正在处理一个使用多个数据库的Go RESTful API应用程序。在启动服务器时,用户会指定他们想要使用的数据库。

在应用程序中,我有三个函数,其中一个处理连接:selectedDb.Get()selectedDb.Add()selectedDb.Connect()

如果有人选择了MySQL,它会处理MySQL相关的事情;如果有人选择了MongoDB,它会处理MongoDB相关的事情,依此类推。

以下是我尝试实现这一目标的方式:

DbInterface.go

  1. package dbinit
  2. type Object struct {
  3. Uuid string
  4. Object string
  5. Deleted bool
  6. }
  7. // 所有连接器都应该具备的接口
  8. type Intfc interface {
  9. Connect() HERE_LIES_MY_PROBLEM
  10. Add(string, string, string) string
  11. Get(string) (Object, bool)
  12. }

MySQL.go

  1. package mysqlConnector
  2. import (
  3. ...
  4. )
  5. type Mysql struct{}
  6. // 连接到MySQL
  7. func (f Mysql) Connect() HERE_LIES_MY_PROBLEM {
  8. client, err = sql.Open("mysql", "yourusername:yourpassword@/yourdatabase")
  9. if err != nil {
  10. panic(err.Error())
  11. }
  12. return client
  13. }
  14. // 向数据库添加项目
  15. func (f Mysql) Add(owner string, refType string, object string) string {
  16. // 处理与该数据库相关的事务
  17. return // 一个字符串
  18. }
  19. func (f Mysql) Get(Uuid string) (dbinit.Object, bool) {
  20. // 处理与该数据库相关的事务
  21. return // 一个对象和一个布尔值
  22. }

Mongo.go

  1. package mongoConnector
  2. import (
  3. ...
  4. )
  5. type Mongo struct{}
  6. // 连接到MongoDB
  7. func (f Mongo) Connect() HERE_LIES_MY_PROBLEM {
  8. info := &mgo.DialInfo{
  9. Addrs: []string{hosts},
  10. Timeout: 60 * time.Second,
  11. Database: database,
  12. Username: username,
  13. Password: password,
  14. }
  15. client, err := mgo.DialWithInfo(info)
  16. if err != nil {
  17. panic(err)
  18. }
  19. return client
  20. }
  21. // 向数据库添加项目
  22. func (f Mongo) Add(owner string, refType string, object string) string {
  23. // 处理与该数据库相关的事务
  24. return // 一个字符串
  25. }
  26. func (f Mongo) Get(Uuid string) (dbinit.Object, bool) {
  27. // 处理与该数据库相关的事务
  28. return // 一个对象和一个布尔值
  29. }

main.go

  1. ...
  2. var selectedDb dbinit.Intfc
  3. commandLineInput := "mysql" // 仅作为示例
  4. if commandLineInput == "mysql" {
  5. selectedDb = mysqlConnector.Mysql{}
  6. } else if commandLineInput == "mongo" {
  7. selectedDb = mongoConnector.Mongo{}
  8. }
  9. client := selectedDb.Connect()
  10. // 每次调用API时都会运行这里
  11. api.HandlerFoobar = foobar.handlerFunction(func(params foobar.Params) middleware.Responder {
  12. // 在这里,我想向selectedDb添加一些内容
  13. selectedDb.Get(client, addStringA, addStringB, addStringC)
  14. return // API响应
  15. })
  16. ...

问题陈述

当我返回MySQL的client时,它对于MongoDB不起作用,反之亦然。

我希望只在启动服务器时连接到数据库,并将client存储在client变量中。然而,问题在于MongoDB返回的client与MySQL返回的client不同。

  1. 在代码中的HERE_LIES_MY_PROBLEM位置应该填写什么?
  2. 或者,我是否对处理这些事情的Go范式有误解?
英文:

I'm working on a Go RESTful API application with multiple databases. When starting the server, the user supplies which database they would like to use.

In the application, I have three functions of which one handles the connection: selectedDb.Get(), selectedDb.Add(), selectedDb.Connect().

If somebody selects, Mysql, it handles things for Mysql, if someone selects MongoDB it handles things for Mongo and so on.

This is how I try to accomplish this:

DbInterface.go

  1. package dbinit
  2. type Object struct {
  3. Uuid string
  4. Object string
  5. Deleted bool
  6. }
  7. // The interface that all connectors should have
  8. type Intfc interface {
  9. Connect() HERE_LIES_MY_PROBLEM
  10. Add(string, string, string) string
  11. Get(string) (Object, bool)
  12. }

MySQL.go

  1. package mysqlConnector
  2. import (
  3. ...
  4. )
  5. type Mysql struct{}
  6. // Connect to mysql
  7. func (f Mysql) Connect() HERE_LIES_MY_PROBLEM {
  8. client, err = sql.Open("mysql", "yourusername:yourpassword@/yourdatabase")
  9. if err != nil {
  10. panic(err.Error())
  11. }
  12. return client
  13. }
  14. // Add item to DB
  15. func (f Mysql) Add(owner string, refType string, object string) string {
  16. // do stuff related to this DB
  17. return // a string
  18. }
  19. func (f Mysql) Get(Uuid string) (dbinit.Object, bool) {
  20. // do stuff related to this DB
  21. return // an object and a bool
  22. }

Mongo.go

  1. package mongoConnector
  2. import (
  3. ...
  4. )
  5. type Mongo struct{}
  6. // Connect to mongo
  7. func (f Mongo) Connect() HERE_LIES_MY_PROBLEM {
  8. info := &mgo.DialInfo{
  9. Addrs: []string{hosts},
  10. Timeout: 60 * time.Second,
  11. Database: database,
  12. Username: username,
  13. Password: password,
  14. }
  15. client, err := mgo.DialWithInfo(info)
  16. if err != nil {
  17. panic(err)
  18. }
  19. return client
  20. }
  21. // Add item to DB
  22. func (f Mongo) Add(owner string, refType string, object string) string {
  23. // do stuff related to this DB
  24. return // a string
  25. }
  26. func (f Mongo) Get(Uuid string) (dbinit.Object, bool) {
  27. // do stuff related to this DB
  28. return // an object and a bool
  29. }

main.go

  1. ...
  2. var selectedDb dbinit.Intfc
  3. commandLineInput := "mysql" // just for the example
  4. if commandLineInput == "mysql" {
  5. selectedDb = mysqlConnector.Mysql{}
  6. } else if commandLineInput == "mongo" {
  7. selectedDb = mongoConnector.Mongo{}
  8. }
  9. client := selectedDb.Connect()
  10. // this runs everytime the API is called
  11. api.HandlerFoobar = foobar.handlerFunction(func(params foobar.Params) middleware.Responder {
  12. // Here I want to add something to the selected dbinit
  13. selectedDb.Get(client, addStringA, addStringB, addStringC)
  14. return // the API response
  15. })
  16. ...

Problem statement

When I return the client for Mysql, it doesn't work for Mongo and visa versa.

I want to connect to the database ONLY when starting the server and store the client inside the client variable. The problem, however, is that Mongo returns another client than Mysql does and so forth.

  1. What should be in the places where I have HERE_LIES_MY_PROBLEM in the code?
  2. Or do I get the Go paradigm wrong for dealing with these things?

答案1

得分: 5

如果你想保留这些方法与接口的关联,你需要稍微修改接口的定义:

接口:

  1. // 所有连接器都应该具有的接口
  2. type Intfc interface {
  3. // 连接到数据库,如果连接时发生错误,返回错误
  4. Connect() error
  5. // 添加数据,返回一个字符串,如果发生错误,返回错误
  6. Add(string, string, string) (string, error)
  7. // 根据参数获取数据,返回一个对象和一个布尔值
  8. Get(string) (Object, bool)
  9. }

MySQL:

  1. type Mysql struct{
  2. conn *sql.DB // 数据库连接
  3. }
  4. // 连接到 MySQL
  5. func (f *Mysql) Connect() error {
  6. conn, err := sql.Open("mysql", "yourusername:yourpassword@/yourdatabase")
  7. if err != nil {
  8. return err
  9. }
  10. f.conn = conn
  11. return nil
  12. }
  13. // 向数据库添加数据
  14. func (f *Mysql) Add(owner string, refType string, object string) (string, error) {
  15. // 做一些操作
  16. return // 返回一个字符串和错误
  17. }
  18. func (f *Mysql) Get(Uuid string) (dbinit.Object, bool) {
  19. // 做一些操作
  20. return // 返回一个对象和一个布尔值
  21. }

Mongo:

  1. type Mongo struct{
  2. session *mgo.Session
  3. }
  4. // 连接到 MongoDB
  5. func (f *Mongo) Connect() error {
  6. info := &mgo.DialInfo{
  7. // 一些数据
  8. }
  9. session, err := mgo.DialWithInfo(info)
  10. if err != nil {
  11. return err
  12. }
  13. f.session = session
  14. return nil
  15. }
  16. // 向数据库添加数据
  17. func (f *Mongo) Add(owner string, refType string, object string) (string, error) {
  18. // 做一些操作
  19. return // 返回一个字符串和错误(成功时可能为 nil)
  20. }
  21. func (f *Mongo) Get(Uuid string) (dbinit.Object, bool) {
  22. // 做一些操作
  23. return // 返回一个对象和一个布尔值
  24. }

主程序:

  1. var selectedDb dbinit.Intfc
  2. commandLineInput := "mysql"
  3. if commandLineInput == "mysql" {
  4. selectedDb = &mysqlConnector.Mysql{}
  5. } else if commandLineInput == "mongo" {
  6. selectedDb = &mongoConnector.Mongo{}
  7. }
  8. err := selectedDb.Connect()
  9. if err != nil {
  10. panic(err)
  11. }
  12. // 每次调用 API 时都会运行这段代码
  13. api.HandlerFoobar = foobar.handlerFunction(func(params foobar.Params) middleware.Responder {
  14. data, err := selectedDb.Add(addStringA, addStringB, addStringC)
  15. if err != nil {
  16. // 做一些操作
  17. }
  18. return // API 响应
  19. })

但你也可以从 Intfc 接口中移除 Connect() error 方法,只使用 AddGet 方法,但你需要更新包中的代码:

MySQL:

  1. // 连接到 MySQL,可以使用任何函数名
  2. func Connect() (*Mysql, error) {
  3. connection, err := sql.Open("mysql", "yourusername:yourpassword@/yourdatabase")
  4. if err != nil {
  5. return nil, err
  6. }
  7. return &Mysql{conn: connection}, nil
  8. }

Mongo:

  1. // 连接到 MongoDB,可以使用任何函数名
  2. func Connect() (*Mongo, error) {
  3. info := &mgo.DialInfo{
  4. // 一些数据
  5. }
  6. s, err := mgo.DialWithInfo(info)
  7. if err != nil {
  8. return nil, err
  9. }
  10. return &Mongo{session: s}, nil
  11. }

主程序:

  1. var selectedDb dbinit.Intfc
  2. var err error
  3. commandLineInput := "mysql"
  4. if commandLineInput == "mysql" {
  5. selectedDb, err = mysqlConnector.Connect()
  6. } else if commandLineInput == "mongo" {
  7. selectedDb, err = mongoConnector.Connect()
  8. }
  9. if err != nil {
  10. panic(err)
  11. }
英文:

If you want to preserve the interface with those methods, you should change a little your interface as:

Interface:

<!-- language: go -->

  1. // The interface that all connectors should have
  2. type Intfc interface {
  3. // Connect to the database, if an error occur at the moment
  4. // of connection, return the error
  5. Connect() error
  6. // Add returns a string, it returns an error if something occurs
  7. Add(string, string, string) (string, error)
  8. Get(string) (Object, bool)
  9. }

MySQL:

<!-- language: go -->

  1. type Mysql struct{
  2. conn *sql.DB // contains the connection to the DB
  3. }
  4. // Connect to mysql
  5. func (f *Mysql) Connect() error {
  6. conn, err := sql.Open(&quot;mysql&quot;, &quot;yourusername:yourpassword@/yourdatabase&quot;)
  7. if err != nil {
  8. return error
  9. }
  10. f.conn = conn
  11. return nil
  12. }
  13. // Add item to DB
  14. func (f *Mysql) Add(owner string, refType string, object string) (string, error) {
  15. // do something
  16. return // a string and error
  17. }
  18. func (f *Mysql) Get(Uuid string) (dbinit.Object, bool) {
  19. // do something
  20. return // an object and a bool
  21. }

Mongo:

<!--language: go -->

  1. type Mongo struct{
  2. session *mgo.Session
  3. }
  4. // Connect to mongo
  5. func (f *Mongo) Connect() error {
  6. info := &amp;mgo.DialInfo{
  7. // some data
  8. }
  9. session, err := mgo.DialWithInfo(info)
  10. if err != nil {
  11. return error
  12. }
  13. f.session = session
  14. return nil
  15. }
  16. // Add item to DB
  17. func (f *Mongo) Add(owner string, refType string, object string) (string, error) {
  18. // do something
  19. return // a string and error (it could be nil at success)
  20. }
  21. func (f *Mongo) Get(Uuid string) (dbinit.Object, bool) {
  22. // do something
  23. return // an object and a bool
  24. }

Main:

<!-- language: go -->

  1. var selectedDb dbinit.Intfc
  2. commandLineInput := &quot;mysql&quot;
  3. if commandLineInput == &quot;mysql&quot; {
  4. selectedDb = &amp;mysqlConnector.Mysql{}
  5. } else if commandLineInput == &quot;mongo&quot; {
  6. selectedDb = &amp;mongoConnector.Mongo{}
  7. }
  8. err := selectedDb.Connect()
  9. if err != nil {
  10. panic(err)
  11. }
  12. // this runs everytime the API is called
  13. api.HandlerFoobar = foobar.handlerFunction(func(params foobar.Params) middleware.Responder {
  14. data, err := selectedDb.Add(addStringA, addStringB, addStringC)
  15. if err != nil {
  16. // do something
  17. }
  18. return // the API response
  19. })

But you also can remove the Connect() error method from Intfc and just use Add and Get, but you should update the packages like:

Mysql

<!-- language: go -->

  1. // Connect to mysql, it could be any function name
  2. func Connect() (*Mysql, error) {
  3. connection, err := sql.Open(&quot;mysql&quot;, &quot;yourusername:yourpassword@/yourdatabase&quot;)
  4. if err != nil {
  5. return nil, error
  6. }
  7. return &amp;Mysql{conn: connection}
  8. }

Mongo

<!-- language: go -->

  1. // Connect to mongo, it could be any function name
  2. func Connect() (*Mongo, error) {
  3. info := &amp;mgo.DialInfo{
  4. // some data
  5. }
  6. s, err := mgo.DialWithInfo(info)
  7. if err != nil {
  8. return nil, error
  9. }
  10. return &amp;Mongo{session: s}
  11. }

Main

<!-- language: go -->

  1. var selectedDb dbinit.Intfc
  2. var err error
  3. commandLineInput := &quot;mysql&quot;
  4. if commandLineInput == &quot;mysql&quot; {
  5. selectedDb, err = mysqlConnector.Connect()
  6. } else if commandLineInput == &quot;mongo&quot; {
  7. selectedDb, err = mongoConnector.Connect()
  8. }
  9. if err != nil {
  10. panic(err)
  11. }

答案2

得分: 1

详细说明一下我的评论,而不是

  1. type Intfc interface {
  2. Connect() HERE_LIES_MY_PROBLEM
  3. Add(string, string, string) string
  4. Get(string) (Object, bool)
  5. }

你可以使用

  1. type Intfc interface {
  2. Connect() DBClient
  3. }

  1. type DBClient interface {
  2. Add(string, string, string) string
  3. Get(string) (Object, bool)
  4. }
  5. type MySQLClient sql.DB
  6. type MongoClient mgo.Session
  7. func (f Mysql) Connect() DBCLient {
  8. client, err = sql.Open("mysql", "yourusername:yourpassword@/yourdatabase")
  9. if err != nil {
  10. panic(err.Error())
  11. }
  12. return MySQLClient(client)
  13. }
  14. func (f Mongo) Connect() DBClient {
  15. info := &mgo.DialInfo{
  16. Addrs: []string{hosts},
  17. Timeout: 60 * time.Second,
  18. Database: database,
  19. Username: username,
  20. Password: password,
  21. }
  22. client, err := mgo.DialWithInfo(info)
  23. if err != nil {
  24. panic(err)
  25. }
  26. return MongoClient(client)
  27. }
  28. func (s *MySQLClient) Add(...) {
  29. // ...
  30. }
  31. func (s *MongoClient) Add(...) {
  32. // ...
  33. }
英文:

Elaborating on my comment, instead of

  1. type Intfc interface {
  2. Connect() HERE_LIES_MY_PROBLEM
  3. Add(string, string, string) string
  4. Get(string) (Object, bool)
  5. }

you can use

  1. type Intfc interface {
  2. Connect() DBClient
  3. }

and

  1. type DBClient interface {
  2. Add(string, string, string) string
  3. Get(string) (Object, bool)
  4. }
  5. type MySQLClient sql.DB
  6. type MongoClient mgo.Session
  7. func (f Mysql) Connect() DBCLient {
  8. client, err = sql.Open(&quot;mysql&quot;, &quot;yourusername:yourpassword@/yourdatabase&quot;)
  9. if err != nil {
  10. panic(err.Error())
  11. }
  12. return MySQLClient(client)
  13. }
  14. func (f Mongo) Connect() DBClient {
  15. info := &amp;mgo.DialInfo{
  16. Addrs: []string{hosts},
  17. Timeout: 60 * time.Second,
  18. Database: database,
  19. Username: username,
  20. Password: password,
  21. }
  22. client, err := mgo.DialWithInfo(info)
  23. if err != nil {
  24. panic(err)
  25. }
  26. return MongoClient(client)
  27. }
  28. func (s *MySQLClient) Add(...) {
  29. // ...
  30. }
  31. func (s *MongoClient) Add(...) {
  32. // ...
  33. }

答案3

得分: 1

我认为,接口Intfc(或更好的名称DbIntfc)应该只有Get和Add两个方法。

而且应该存在另一个函数 - 但不是DbIntfc的一部分,它返回连接到MySql或MongoDb的DbIntfc。让我们看一下:

  1. type MySqlDbIntfc struct{
  2. db *Sql.DB
  3. }
  4. // 连接到mysql
  5. func NewMySqlDbIntfc() (DbIntfc,error) {
  6. // 请不要在这样的抽象方法中使用panic
  7. client, err := sql.Open("mysql", "yourusername:yourpassword@/yourdatabase")
  8. if err != nil {
  9. return nil, err
  10. }
  11. return &MySqlDbIntfc{client}, nil
  12. }
  13. func (mySqlDb *MySqlDbIntfc) Get(Uuid string) (dbinit.Object, error) {
  14. var obj dbinit.Object
  15. err := mySqlDb.db.QueryRow("SELECT uuid, object, deleted FROM myTable WHERE uuid=?", Uuid).Scan(&obj.Uuid, &obj.Object, &obj.Deleted)
  16. if err != nil {
  17. return dbinit.Object{}, err
  18. }
  19. return obj, nil
  20. }

实现NewMgoDbIntfc应该很简单,包括NewMgoDbIntfc.Add/Get方法。

而且决定是使用NewMySqlDbIntfc还是NewMgoDbIntfc也应该很容易。

英文:

I think, that interface Intfc (or better name DbIntfc) should have only methods Get and Add.

And it should exist another function - but not a part of DbIntfc, that returns DbIntfc - that connects to MySql or MongoDb. Let's look:

  1. type MySqlDbIntfc struct{
  2. db *Sql.DB
  3. }
  4. // Connect to mysql
  5. func NewMySqlDbIntfc() (DbIntfc,error) {
  6. // Please do not prefer panic in such abstract methods
  7. client, err := sql.Open(&quot;mysql&quot;, &quot;yourusername:yourpassword@/yourdatabase&quot;)
  8. if err != nil {
  9. return nil, err
  10. }
  11. return &amp;MySqlDbIntfc{client}, nil
  12. }
  13. func (mySqlDb *MySqlDbIntfc) Get(Uuid string) (dbinit.Object, error) {
  14. var obj dbinit.Object
  15. err := mySqlDb.db.QueryRow(&quot;SELECT uuid, object, deleted FROM myTable WHERE uuid=?&quot;, Uuid).Scan(&amp;obj.Uuid, &amp;obj.Object, &amp;obj.Deleted)
  16. if err != nil {
  17. return dbinit.Object{}, err
  18. }
  19. return obj, nil
  20. }

And implemention NewMgoDbIntfc should be easy, including methods NewMgoDbIntfc.Add/Get.

And decision whether use NewMySqlDbIntfc or NewMgoDbIntfc should also be easy.

答案4

得分: 0

> 1. 在代码中的HERE_LIES_MY_PROBLEM的位置应该填写什么?

根据源代码DialWithInfo()函数返回error*Session,它们都是来自mgo包的结构体。所以你可以将HERE_LIES_MY_PROBLEM替换为*mgo.Session

> 2. 我对处理这些事情的Go范式理解错了吗?

关于连接多个数据库的惯用方法,我认为有很多不同的观点。以下是我对连接Mongo和Redis的一些想法:

  1. var logger *log.Logger
  2. func init() {
  3. logger = log.New(os.Stderr,
  4. "Database :: ",
  5. log.Ldate|log.Ltime|log.Lshortfile)
  6. }
  7. // 在这里创建不同类型的数据库连接。
  8. func SystemConnection() map[string]interface{} {
  9. listConnection := make(map[string]interface{})
  10. var err error
  11. // 创建Redis连接
  12. redisConn := RedisHost{
  13. Address: "localhost:6379",
  14. Password: "",
  15. DB: 0,
  16. }
  17. redisConnection, err := redisConn.Connect()
  18. if err != nil {
  19. panic(err)
  20. }
  21. // 创建MongoDB连接
  22. mongo := MongoHost{
  23. Host: "localhost",
  24. Port: "27017",
  25. }
  26. mongoConnection := mongo.Connect()
  27. listConnection["redis"] = redisConnection
  28. listConnection["mongodb"] = mongoConnection
  29. return listConnection
  30. }
  31. func GetMongo() *mgo.Session {
  32. // 创建MongoDB连接
  33. mongo := MongoHost{
  34. Host: "localhost",
  35. Port: "27017",
  36. }
  37. mongoConnection := mongo.Connect()
  38. return mongoConnection
  39. }

你可以从这里查看源代码。

要使用上述代码,在你的主程序的init()函数中调用SystemConnection(),像这样:

  1. func init(){
  2. listConnection := database.SystemConnection()
  3. // 获取Redis连接,将其转换为*redis.Client类型。
  4. redisConn := listConnection["redis"].(*redis.Client)
  5. // 获取MongoDB连接。
  6. mongoConn := listConnection["mongodb"].(*mgo.Session)
  7. }

再次说明,这种方法是我个人的观点,可能还有其他适合你的方法。

英文:

> 1. What should be in the places where I have HERE_LIES_MY_PROBLEM in the code?

As you can see from the source that the DialWithInfo() return error and *Session from mgo package and that is a struct. so you can replace your HERE_LIES_MY_PROBLEM with *mgo.Session

> 2. do I get the Go paradigm wrong for dealing with these things?

As far as the idiomatic to connect with multiple databases there are many opinion I think. And here is some of my thought to connect with mongo and redis :

  1. var logger *log.Logger
  2. func init() {
  3. logger = log.New(os.Stderr,
  4. &quot;Database :: &quot;,
  5. log.Ldate|log.Ltime|log.Lshortfile)
  6. }
  7. //we create different types of databse connection here.
  8. func SystemConnection() map[string]interface{} {
  9. listConnection := make(map[string]interface{})
  10. var err error
  11. // create redis connection
  12. redisConn := RedisHost{
  13. Address: &quot;localhost:6379&quot;,
  14. Password: &quot;&quot;,
  15. DB: 0,
  16. }
  17. redisConnection, err := redisConn.Connect()
  18. if err != nil {
  19. panic(err)
  20. }
  21. //create mongodb connection
  22. mongo := MongoHost{
  23. Host: &quot;localhost&quot;,
  24. Port: &quot;27017&quot;,
  25. }
  26. mongoConnection := mongo.Connect()
  27. listConnection[&quot;redis&quot;] = redisConnection
  28. listConnection[&quot;mongodb&quot;] = mongoConnection
  29. return listConnection
  30. }
  31. func GetMongo() *mgo.Session {
  32. //create mongodb connection
  33. mongo := MongoHost{
  34. Host: &quot;localhost&quot;,
  35. Port: &quot;27017&quot;,
  36. }
  37. mongoConnection := mongo.Connect()
  38. return mongoConnection
  39. }

you can see the source from here

To use above code you can call the SystemConnection() on your init() in your main program. like this :

  1. func init(){
  2. listConnection := database.SystemConnection()
  3. //getting redis connection convert it from interface to *redisClient.
  4. redisConn := listConnection[&quot;redis&quot;].(*redis.Client)
  5. // get postgre connection.
  6. mongoConn := listConnection[&quot;mongodb&quot;].(*mgo.Session)
  7. }

Again this approach is my opinion there are others that might be suit with yours.

huangapple
  • 本文由 发表于 2017年5月3日 17:17:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/43755892.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定