gpt4 book ai didi

golang gocql.NewCluster 未定义没有字段或方法

转载 作者:IT王子 更新时间:2023-10-29 02:04:48 30 4
gpt4 key购买 nike

我正在尝试查询测试键空间,例如:

package main

import "fmt"
import _ "github.com/gocql/gocql"

var (
gocql string
)

func main() {
// connect to the cluster
cluster := gocql.NewCluster("127.0.0.1")
cluster.Keyspace = "dbaccess"
session, _ := cluster.CreateSession()
defer session.Close()

if err := session.Query("SELECT name, age FROM people WHERE name='doug'").Scan(&name, &age); err != nil {
log.Fatal(err)
}
fmt.Println(name, age)
}

但我收到如下错误:

12: gocql.NewCluster undefined (type string has no field or method NewCluster)

这是否意味着它试图指向 gocql/gocql 文件夹中的方法但找不到它,或者导入内容的语法错误或?

最佳答案

我认为您的问题是您在此处将 gocql var 声明为字符串:

var (
gocql string
)

您应该只删除它,它应该可以解决该特定问题。

此外您的导入声明:

import  _ "github.com/gocql/gocql"

不应包含下划线 (_),因为您明确使用了 gocql 而不仅仅是导入它的副作用。

关于golang gocql.NewCluster 未定义没有字段或方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34779824/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com