Connect to Cassandra with Golang

Menu

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 get github.com/gocql/gocql

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 console.instaclustr.com > Log In > Cluster Details > Connection Info > Default Credentials for Password Authentication.

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

 

FREE TRIAL

Spin up a cluster in less
than 5 minutes.
(No credit card required)

Sign Up Now
Close

Site by Swell Design Group