Created
May 17, 2022 15:32
-
-
Save fbiville/a890af2eaa39a3a2f4acf8a625faa1e5 to your computer and use it in GitHub Desktop.
Simple Neo4j program
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"github.com/neo4j/neo4j-go-driver/v4/neo4j" | |
"io" | |
"os" | |
"strings" | |
) | |
func main() { | |
driver, err := neo4j.NewDriver(os.Args[1], neo4j.BasicAuth(os.Args[2], os.Args[3], "")) | |
if err != nil { | |
panic(err) | |
} | |
defer handleClose(driver) | |
session := driver.NewSession(neo4j.SessionConfig{}) | |
defer handleClose(session) | |
result, err := session.ReadTransaction(sayHello) | |
if err != nil { | |
panic(err) | |
} | |
fmt.Printf("Program says: %q", result) | |
} | |
func sayHello(tx neo4j.Transaction) (any, error) { | |
result, err := tx.Run( | |
`RETURN reduce(acc = "", letter IN $letters | acc + letter) AS hello`, | |
map[string]interface{}{ | |
"letters": strings.Split("Hello, GraphConnect!", ""), | |
}) | |
if err != nil { | |
return nil, err | |
} | |
record, err := result.Single() | |
if err != nil { | |
return nil, err | |
} | |
rawHello, found := record.Get("hello") | |
if !found { | |
return nil, fmt.Errorf("expected 'hello' column in result, none found") | |
} | |
hello, ok := rawHello.(string) | |
if !ok { | |
return nil, fmt.Errorf("expected 'hello' column to be a string, but was not") | |
} | |
return hello, nil | |
} | |
func handleClose(closer io.Closer) { | |
if err := closer.Close(); err != nil { | |
panic(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment