英文:
golang gocql.NewCluster undefined no field or method
问题
我正在尝试查询一个名为test keyspace的键空间,代码如下:
package main
import "fmt"
import _ "github.com/gocql/gocql"
var (
gocql string
)
func main() {
// 连接到集群
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文件夹中的方法,但找不到它,或者导入语法有问题?
英文:
I'm trying to query a test keyspace like:
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)
}
But I get an error like:
12: gocql.NewCluster undefined (type string has no field or method NewCluster)
Does that mean it's trying to point to the method in the gocql/gocql folder but can't find it, or is the syntax wrong to import stuff or?
答案1
得分: 2
我认为你的问题是在这里将一个 gocql 变量声明为字符串:
var (
gocql string
)
你应该将其移除,这样应该就能解决这个特定的问题。
此外,你的导入语句:
import _ "github.com/gocql/gocql"
不应该包含下划线(_
),因为你明确地使用了 gocql,而不仅仅是为了导入其副作用。
英文:
I think your problem is that you are declaring a gocql var as a string here:
<!-- language: go -->
var (
gocql string
)
You should just remove this and it should resolve that particular issue.
In addition your import statement:
<!-- language: go -->
import _ "github.com/gocql/gocql"
Shouldn't include an underscore (_
) since you are explicitly using gocql and not just importing for its side effects.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论