11package main
2+
23import (
4+ "context"
35"fmt"
4- "github.com/neo4j/neo4j-go-driver/neo4j"
6+ "github.com/neo4j/neo4j-go-driver/v5/ neo4j"
57)
8+
69func main () {
7- var driver neo4j.Driver
8- var err error
9- // Aura requires you to use "bolt+routing" protocol, and process your queries using an encrypted connection
10+ ctx := context .Background ()
11+ // Aura requires you to use "neo4j+s" scheme, so that your queries are processed using an encrypted connection
1012// (You may need to replace your connection details, username and password)
11- boltURL := "bolt+routing ://<Bolt url for Neo4j Aura database>"
13+ uri := "neo4j+s ://<Bolt url for Neo4j Aura database>"
1214auth := neo4j .BasicAuth ("<Username for Neo4j Aura database>" , "<Password for Neo4j Aura database>" , "" )
13-
14- configurers := []func (* neo4j.Config ){
15- func (config * neo4j.Config ) {
16- config .Encrypted = true
17- },
18- }
19- if driver , err = neo4j .NewDriver (boltURL , auth , configurers ... ); err != nil {
15+ driver , err := neo4j .NewDriverWithContext (uri , auth )
16+ if err != nil {
2017panic (err )
2118}
22-
2319// Don't forget to close the driver connection when you are finished with it
24- defer driver .Close ()
25-
26- var writeSession neo4j.Session
27- // Using write transactions allow the driver to handle retries and transient errors for you
28- if writeSession , err = driver .Session (neo4j .AccessModeWrite ); err != nil {
29- panic (err )
30- }
31- defer writeSession .Close ()
20+ defer closeResource (ctx , driver )
3221
3322// To learn more about the Cypher syntax, see https://neo4j.com/docs/cypher-manual/current/
3423// The Reference Card is also a good resource for keywords https://neo4j.com/docs/cypher-refcard/current/
@@ -37,52 +26,54 @@ func main() {
3726MERGE (p2:Person { name: $person2_name })
3827MERGE (p1)-[:KNOWS]->(p2)
3928RETURN p1, p2`
40-
41- var result neo4j.Result
42- result , err = writeSession .Run (createRelationshipBetweenPeopleQuery , map [string ]interface {}{
29+ params := map [string ]any {
4330"person1_name" : "Alice" ,
4431"person2_name" : "David" ,
45- })
32+ }
4633
34+ // Using ExecuteQuery allows the driver to handle retries and transient errors for you
35+ result , err := neo4j .ExecuteQuery (ctx , driver , createRelationshipBetweenPeopleQuery , params ,
36+ neo4j .EagerResultTransformer )
4737if err != nil {
4838panic (err )
4939}
50-
51- // You should capture any errors along with the query and data for traceability
52- if result .Err () != nil {
53- panic (result .Err ())
40+ for _ , record := range result .Records {
41+ fmt .Printf ("First: '%s'\n " , getPersonName (record , "p1" ))
42+ fmt .Printf ("Second: '%s'\n " , getPersonName (record , "p2" ))
5443}
5544
56- for result .Next () {
57- firstPerson := result .Record ().GetByIndex (0 ).(neo4j.Node )
58- fmt .Printf ("First: '%s'\n " , firstPerson .Props ()["name" ].(string ))
59- secondPerson := result .Record ().GetByIndex (1 ).(neo4j.Node )
60- fmt .Printf ("Second: '%s'\n " , secondPerson .Props ()["name" ].(string ))
61- }
62-
63- var readSession neo4j.Session
64-
65- if readSession , err = driver .Session (neo4j .AccessModeRead ); err != nil {
66- panic (err )
67- }
68- defer readSession .Close ()
69-
7045readPersonByName := `
7146MATCH (p:Person)
7247WHERE p.name = $person_name
7348RETURN p.name AS name`
74-
75- result , err = readSession .Run (readPersonByName , map [string ]interface {}{"person_name" : "Alice" })
76-
49+ result , err = neo4j .ExecuteQuery (ctx , driver , readPersonByName , map [string ]any {"person_name" : "Alice" },
50+ neo4j .EagerResultTransformer )
7751if err != nil {
7852panic (err )
7953}
54+ for _ , record := range result .Records {
55+ name , _ , err := neo4j .GetRecordValue [string ](record , "name" )
56+ if err != nil {
57+ panic (err )
58+ }
59+ fmt .Printf ("Person name: '%s' \n " , name )
60+ }
61+ }
8062
81- if result .Err () != nil {
82- panic (result .Err ())
63+ func closeResource (ctx context.Context , closer interface { Close (context.Context ) error }) {
64+ if err := closer .Close (ctx ); err != nil {
65+ panic (err )
8366}
67+ }
8468
85- for result .Next () {
86- fmt .Printf ("Person name: '%s' \n " , result .Record ().GetByIndex (0 ).(string ))
69+ func getPersonName (record * neo4j.Record , key string ) string {
70+ firstPerson , _ , err := neo4j .GetRecordValue [neo4j.Node ](record , key )
71+ if err != nil {
72+ panic (err )
73+ }
74+ firstPersonName , err := neo4j .GetProperty [string ](firstPerson , "name" )
75+ if err != nil {
76+ panic (err )
8777}
78+ return firstPersonName
8879}
0 commit comments