golang gocql.NewCluster undefined 没有字段或方法

golang gocql.NewCluster undefined no field or method

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

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 而不仅仅是导入它的副作用。