Let’s Go with Neo4j
Setting up and using the Neo4j Go Driver
It’s been a while since the official Neo4j driver is available for Go. So I thought of giving it a try.
I’m used to working with windows but recently I started using Mac. And got used to problems while installing new software.
Though installing the Neo4j application is a straight forward job, establishing the connectivity from my Go project to Neo4j DB was tedious. There are a lot of things to be done to make it work. I found it quite difficult installing these drivers, other essential libraries and bridging the gap between them. So I’ve broken down the process and made it easy.
The Go driver is built on top of the C Connector, Seabolt. So we need to have a cgo development environment.
Let’s get started with our work !!!
Linux provides pkg-config as part of the package management system. But, yeah you are right, Mac os doesn't. It’s simple to install though.
brew install pkg-config
Clone the Seabolt source from github. To build the project you’ll be needing a few libraries as well.
brew install cmake
brew install openssl
Create an environment variable named OPEN_SSL_ROOT_DIR. that points to the openssl library installation path (default is /usr/local/opt/openssl)
I keep all my vendor libraries in /Users/username/libraries. I’ve cloned the Seabolt source to the same path. You can proceed with your preferred directory.
Add to the path
Add the following two variables to the profile
export PKG_CONFIG_PATH = /Users/username/libraries/seabolt-1.7/build/dist/share/pkgconfig
You can make sure if the Seabolt library has been added to the pkg-config.
If you can see seabolt17-static seabolt17-static — The C Connector Library for Neo4j in the list of libraries, you are good to go.
Neo4j golang driver
Install the Neo4j golang driver.
go get github.com/neo4j/neo4j-go-driver/neo4j
We are done with all the requirements.
Let’s check whether our efforts have paid off
Start your Neo4j desktop application and get the bolt URL, username and password for your graph DB.
I’ve created a Graph named “neo4j”, password as “password” and it can be accessed in “bolt://localhost:7687” URL.
Create a file neo4jgolang.go. Add the following two code snippets to it. The code is self-explanatory.
I assume you’ve followed all the steps above. Now simply go ahead and execute your code
go run neo4jgolang.go
If all goes well you should be able to see the following response in the terminal.
“Welcome to world of Graphs
Greetings from graph : hello, world, from node 0”
In Neo4j browser, you will be able to see a Greeting node created with a message as “hello world”
So that’s it for this post. I’ve covered the basic and essential procedure to make your go project backed by Neo4j.
Hope you like it!!!