I want to connect scylladb on golang and i tried copying the code from connect and pasting it doesn't work

I want to connect scylladb on golang and i tried copying the code from connect and pasting it doesn’t work.

Please share the code you used and the error you received.
You can see a running example of using golang with ScyllaDB on ScyllaDB University.
Here is the first less (part one out of three).

This is the code I came up with and I tried it and it doesn’t work because it doesn’t find it. github.com/gocql/gocql/scyllacloud

package main

import (
“fmt”
“log”

"github.com/gocql/gocql"
"github.com/gocql/gocql/scyllacloud"

)

const (
connectionBundlePath = “./build.yaml”
)

func main() {
cluster, err := scyllacloud.NewCloudCluster(connectionBundlePath)
if err != nil {
log.Fatalf(“Failed to create cloud cluster config: %s”, err)
}
cluster.PoolConfig.HostSelectionPolicy = gocql.DCAwareRoundRobinPolicy(“us-east-1”)

session, err := cluster.CreateSession()
if err != nil {
	log.Fatalf("Failed to connect to cluster: %s", err)
}

defer session.Close()

var query = session.Query("SELECT * FROM mykeyspace.mytable")

if rows, err := query.Iter().SliceMap(); err == nil {
	for _, row := range rows {
		fmt.Printf("%v\n", row)
	}
} else {
	log.Fatalf("Query error: %s", err)
}

}

Are you connecting to ScyllaCloud Serverless cluster (free trial)?
If yes, you probably don’t have a fork replacement directive in your go.mod.

Add the following line to your project go.mod file.

replace github.com/gocql/gocql => github.com/scylladb/gocql v1.7.3

and run

go mod tidy

Then make sure the path to the bundle is correct, and your code should run fine.

1 Like