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

huangapple go评论71阅读模式
英文:

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  _ &quot;github.com/gocql/gocql&quot;

Shouldn't include an underscore (_) since you are explicitly using gocql and not just importing for its side effects.

huangapple
  • 本文由 发表于 2016年1月14日 08:49:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/34779824.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定