Go driver

This section presents the breaking changes between the Neo4j 1.8 Go Driver and 4.x Go Driver.

The latest version of the Go driver for Neo4j can be found on the Go driver’s official page.

  • All import statements need to be changed to "github.com/neo4j/neo4j-go-driver/v4/neo4j".

  • NewSession does not return an error anymore.

  • Result now exposes a Single method, which outputs the only record returned by the query.

  • Record now directly exposes Keys and Values.

Example 1. Example of changes between the 1.8 Go driver and the 4.x Go driver
Example code for the 4.x Go driver Example code for the 1.8 Go driver
package main
import (
	"fmt"
	"github.com/neo4j/neo4j-go-driver/v4/neo4j"
)
func main() {
    // [...]
	session := driver.NewSession(neo4j.SessionConfig{
		AccessMode:   neo4j.AccessModeWrite,
		Bookmarks:    []string{bookmark},
		DatabaseName: "neo4j",
	})
	defer session.Close()
	transaction, err := session.BeginTransaction()
	handleError(err)
	defer transaction.Close()
	result, err := transaction.Run(
		`CREATE (a:Greeting) SET a.message = $message RETURN a.message + ", from node " + id(a)`,
		map[string]interface{}{
			"message": "helloWorld",
		},
	)
	record, err := result.Single()
	handleError(err)
	greeting := record.Values[0]
	fmt.Println(greeting)
	handleError(transaction.Commit())
}
package main
import (
	"fmt"
	"github.com/neo4j/neo4j-go-driver/neo4j"
)
func main() {
    // [...]
	session, err := driver.NewSession(neo4j.SessionConfig{
		AccessMode:   neo4j.AccessModeWrite,
		Bookmarks:    []string{bookmark},
		DatabaseName: "neo4j",
	})
	handleError(err)
	defer session.Close()
	transaction, err := session.BeginTransaction()
	handleError(err)
	defer transaction.Close()
	result, err := transaction.Run(
		`CREATE (a:Greeting) SET a.message = $message RETURN a.message + ", from node " + id(a)`,
		map[string]interface{}{
			"message": "helloWorld",
		},
	)
	record, err := single(result)
	handleError(err)
	greeting := record.Values()[0]
	fmt.Println(greeting)
	handleError(transaction.Commit())
}
func single(result neo4j.Result) (neo4j.Record, error) {
	if !result.Next() {
		return nil, fmt.Errorf("expected at least 1 result, got none")
	}
	record := result.Record()
	if result.Next() {
		return nil, fmt.Errorf("expected exactly 1 result, got at least 1 more")
	}
	return record, nil
}