英文:
I am using postgres database with golang and i am trying to return connection object but I don't know the return type connection object
问题
在上面的示例中,返回类型应该是 *sql.DB
。
英文:
func (t *DbConnection) Connect() (return type) {
dbTest, err := sql.Open("postgres", "user = praveen dbname = test_twichblade sslmode = disable")
return dbTest
}
In above example what should be the return type ?
答案1
得分: 2
Open函数返回(*DB, error),所以你应该返回*sql.DB
func Open(driverName, dataSourceName string) (*DB, error)
func (t *DbConnection) Connect() (*sql.DB) {
dbTest, err := sql.Open("postgres", "user = praveen dbname = test_twichblade sslmode = disable")
return dbTest
}
英文:
Open function returns (*DB, error), so you should return *sql.DB
func Open(driverName, dataSourceName string) (*DB, error)
func (t *DbConnection) Connect() (*sql.DB) {
dbTest, err := sql.Open("postgres", "user = praveen dbname = test_twichblade sslmode = disable")
return dbTest
}
答案2
得分: 1
根据https://golang.org/pkg/database/sql/#Open,sql.Open
返回*DB, error
,所以在你的情况下应该返回*sql.DB
。你可以使用这个关于database/sql
包的信息https://golang.org/pkg/database/sql/。
英文:
According to https://golang.org/pkg/database/sql/#Open
sql.Open
returns *DB, error
, so you should return *sql.DB
in your case.
You can use this information https://golang.org/pkg/database/sql/ about databse/sql
package
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论