英文:
Too many arguments in call to "github.com/jackc/pgx".Connect
问题
我正在尝试使用pgx打开与PostgreSQL数据库的连接,但是遇到以下错误:
./dbservice.go:12:26: 调用"github.com/jackc/pgx".Connect时参数过多
已有 (context.Context, string)
期望的是 "github.com/jackc/pgx".ConnConfig
./dbservice.go:13:18: 调用conn.Close时参数过多
已有 (context.Context)
期望的是 ()
./dbservice.go:21:44: 无法将context.Background() (类型为context.Context) 作为参数传递给conn.Query的类型为string的参数
我不确定这个错误要求我做什么。当我从主文件调用pgx.Connect
时,它是有效的,但在这里却不起作用。以下是代码示例:
func initNodes(nodes *[]Node, searchNodes *[]SearchNode, storageNodes *[]StorageNode) error {
conn, err := pgx.Connect(context.Background(), DATABATE_URL)
defer conn.Close(context.Background())
if err != nil {
fmt.Printf("连接失败:%v\n", err)
os.Exit(-1)
}
...
func main() {
a := Arbiter{}
a.init()
}
有什么想法吗?
英文:
I am trying to open a connection to a postgres database using pgx and I am getting the following error:
./dbservice.go:12:26: too many arguments in call to "github.com/jackc/pgx".Connect
have (context.Context, string)
want ("github.com/jackc/pgx".ConnConfig)
./dbservice.go:13:18: too many arguments in call to conn.Close
have (context.Context)
want ()
./dbservice.go:21:44: cannot use context.Background() (type context.Context) as type string in argument to conn.Query
I am not sure what the error is asking me to do here. pgx.Connect
works when I call it from the main file but here it doesn't work. Here's the code:
func initNodes(nodes *[]Node, searchNodes *[]SearchNode, storageNodes *[]StorageNode) error {
conn, err := pgx.Connect(context.Background(), DATABATE_URL)
defer conn.Close(context.Background())
if err != nil {
fmt.Printf("Connection failed: %v\n", err)
os.Exit(-1)
}
...
func main() {
a:= Arbiter{}
a.init()
}
Any ideas?
答案1
得分: 3
很可能你在 dbservice.go
中导入了 v3
的 pgx
API,但在你的“主文件”中导入了 v4
的 API。github.com/jackc/pgx/v4
中的 Connect
函数接受你传递的两个参数。而 v3
中的 Connect
函数则接受一个 pkg.ConnConfig
。
所以,请检查一下 dbservice.go
中的 import
语句:如果你打算使用 v4
的 API,请将其导入为 "github.com/jackc/pgx/v4"
。
英文:
Most likely you are importing the v3
pgx
API in dbservice.go
, but the v4
API in your “main file”. The Connect
function in github.com/jackc/pgx/v4
accepts the two arguments you're passing. The Connect
function in v3
accepts a pkg.ConnConfig
instead.
So, check your import
statements in dbservice.go
: if you intend to be using the v4
API, import it as "github.com/jackc/pgx/v4"
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论