Search code examples
gocassandragocql

golang gocql.NewCluster undefined no field or method


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?


Solution

  • I think your problem is that you are declaring a gocql var as a string here:

    var (
        gocql string
    )
    

    You should just remove this and it should resolve that particular issue.

    In addition your import statement:

    import  _ "github.com/gocql/gocql"
    

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