The Neo4j official Go driver's session object has a ReadTransaction method that you can use for marking read queries explicitly.
For Version 4 of the driver, the below example using the ReadTransaction function is correct:
session := driver.NewSession(neo4j.SessionConfig{})
defer session.Close()
_, err = session.ReadTransaction(func(tx neo4j.Transaction) (interface{}, error) {
getActorsRead := `
MATCH (p:Person)-[:ACTED_IN]->(m)
WHERE toLower(m.title) CONTAINS toLower($title)
RETURN DISTINCT p.name as actorsTx`
result, err := tx.Run(getActorsRead, map[string]interface{}{
"title": "matrix",
})
if err != nil {
return nil, err
}
for result.Next() {
fmt.Printf("Found actor: '%s' \n", result.Record().Values[0].(string))
}
return nil, result.Err()
})
For Version 5 of the driver, you need to use the ExecuteRead function instead:
session := driver.NewSession(neo4j.SessionConfig{})
defer session.Close()
_, err = session.ExecuteRead(func(tx neo4j.Transaction) (interface{}, error) {
getActorsRead := `
MATCH (p:Person)-[:ACTED_IN]->(m)
WHERE toLower(m.title) CONTAINS toLower($title)
RETURN DISTINCT p.name as actorsTx`
result, err := tx.Run(getActorsRead, map[string]interface{}{
"title": "matrix",
})
if err != nil {
return nil, err
}
for result.Next() {
fmt.Printf("Found actor: '%s' \n", result.Record().Values[0].(string))
}
return nil, result.Err()
})
Comments
0 comments
Please sign in to leave a comment.