Installing the driver

Introduction

In the Cypher Fundamentals course, you learned how to query Neo4j using Cypher.

To run Cypher statements in a Go application, you’ll need the Neo4j Go Driver. The driver acts as a bridge between your Go code and Neo4j, handling connections to the database and the execution of Cypher queries.

Setting up a Go Project

First, create a new Go project and initialize it as a module:

shell
mkdir hello-neo4j
cd hello-neo4j
go mod init graphacademy/hello

Installing the Driver

To install the driver, use the go get command:

shell
go get github.com/neo4j/neo4j-go-driver/v5

Creating a Driver Instance

go
package main

import (
    "context"
    "fmt"
    "github.com/neo4j/neo4j-go-driver/v5/neo4j"
)

func main() {
    driver, err := neo4j.NewDriverWithContext(
        "neo4j://localhost:7687", // (1)
        neo4j.BasicAuth("neo4j", "your-password", ""), // (2)
    )
    if err != nil { panic(err) }
    defer driver.Close(context.Background()) // (3)
}

You start by importing the driver and creating an instance using the neo4j.NewDriverWithContext() function:

  1. The connection string for your Neo4j database

  2. Your Neo4j username and password

  3. Always close the driver when done using the defer statement

Best Practice

Create one Driver instance and share it across your entire application.

Verifying Connectivity

You can verify the connection is correct by calling the VerifyConnectivity() method.

go
ctx := context.Background()
err = driver.VerifyConnectivity(ctx)
if err != nil {
    panic(err)
}

Verify Connectivity

The VerifyConnectivity() method will return an error if the connection cannot be made.

Running Your First Query

The ExecuteQuery() method executes a Cypher query and returns the results.

go
ctx := context.Background()
result, err := neo4j.ExecuteQuery(ctx, driver, // (1)
    "RETURN COUNT {()} AS count",
    nil, // (2)
    neo4j.EagerResultTransformer, // (3)
)
if err != nil { panic(err) }

// Get the first record
first := result.Records[0] // (4)
// Print the count entry
count, _ := first.Get("count") // (5)
fmt.Println(count)

What is happening here?

  1. neo4j.ExecuteQuery() runs a Cypher query to get the count of all nodes in the database

  2. nil means no parameters are passed to the query

  3. EagerResultTransformer loads all results into memory

  4. Records contains a slice of the rows returned

  5. Values from the RETURN clause are accessed using the Get() method

Full driver lifecycle

It is good practice to close the driver when you are finished with it. You can use the defer statement to automatically close the driver when the function exits.

You can also explicitly call Close() to release any resources held by the driver.

go
driver, err := neo4j.NewDriverWithContext(
    "neo4j://localhost:7687",
    neo4j.BasicAuth("neo4j", "your-password", ""),
)
if err != nil {
    panic(err)
}
defer driver.Close(context.Background())
go
ctx := context.Background()
driver.Close(ctx)

Lesson Summary

In this lesson you learned how to install the Neo4j Go Driver, create a Driver instance, verify connectivity to your database, and execute your first Cypher statement.

For async applications, use the async API methods.

In the next lesson, you will take a quiz to test your knowledge of installing and creating a driver instance.

Chatbot

How can I help you today?