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

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

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

huangapple
  • 本文由 发表于 2015年10月9日 19:10:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/33036796.html
匿名

发表评论

匿名网友

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

确定