Connect to Apache Cassandra with Golang

In this article, we will introduce how to connect to a Cassandra cluster using Go. We will start by installing Go and the Go Cassandra driver and then provide simple demo code for connecting remote server (Cassandra) and local program.

Download the Golang programming language

Go to Golang.org and choose an appropriate version to download.

Install Golang

Follow the instructions to install.

Install the Cassandra Golang driver

Open a terminal and run the following command:

go install github.com/gocql/gocql@latest

Install the GoLand IDE

Go to Jetbrains and install the GoLand IDE using the 30 day free trial.

Create a new project

Open GoLand and select ‘New Project’.

Choose a name for your project when prompted. In this example we have chosen the name “my_go_driver” but you can use whatever name you prefer, it shouldn’t affect the process.

If the SDK is “<No SDK>” then point the project to the Go SDK.  The SDK should be in the directory /usr/local/go. Once done, click “Create”.

Right click the project name in the side-bar and generate a new Golang file.

In the pop up choose a name for the new Go file. In these examples we’ll be using “driver” as the file name.

Write Sample Code

Take the following sample code and replace the “Public IP” with the public IP addresses used by your cluster. Also replace “Username” and “Password” with your cluster’s authentication settings. To get your cluster’s username and password settings, go to the Instaclustr Console > Log In > Cluster Details > Connection Info > Default Credentials for Password Authentication.

Click Run > Run… then press Enter and you should get the following output:

 

By Instaclustr Support
Need Support?
Experiencing difficulties on the website or console?
Already have an account?
Need help with your cluster?
Contact Support
Why sign up?
To experience the ease of creating and managing clusters via the Instaclustr Console
Spin up a cluster in minutes