返回的参数过多 [英] Too many arguments to return
问题描述
我有这个golang文件:
I have this golang file:
package main
import (
"log"
"sync"
"github.com/jmoiron/sqlx"
)
var db *sqlx.DB
var once sync.Once
// GetDBConnection whatever
func GetDBConnection() {
once.Do(func() {
db, err := sqlx.Connect("postgres", "user=tom dbname=jerry password=myPassword sslmode=disable")
if err != nil {
log.Fatalln(err)
}
})
return db // <<< error here
}
我收到此错误:
要返回的参数太多
我只是试图创建一个单例模式并返回db连接.我不确定sqlx.Connect返回的类型是否为sqlx.DB,这可能是问题所在.有没有一种快速的方法来确定 sqlx.Connect()
的返回类型?
I am just trying to create a singleton pattern and return the db connection. I am not sure if what is returned from sqlx.Connect is type sqlx.DB, that might be the problem. Is there a quick way to determine the return type of sqlx.Connect()
?
推荐答案
您已声明函数 GetDBConnection()
不返回任何参数.
You've declared the function GetDBConnection()
to return no arguments.
func GetDBConnection() {
您必须告诉Go 您打算返回的参数类型:>
You have to tell Go the type of the argument you intend to return:
func GetDBConnection() *sqlx.DB {
关于确定类型,我只是去看一下源代码.您也可以查看godoc.org上的文档,该文档是自动从公开可用的Go软件包生成.
As for determining the type, I just went to look at the source code. You could also look at the documentation on godoc.org, which is auto-generated from publicly available Go packages.
这篇关于返回的参数过多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!