@@ -26,73 +26,84 @@ The complete `redisgraph-go` API is documented on [GoDoc](https://godoc.org/gith
2626package main
2727
2828import (
29- " fmt"
30- " github.com/gomodule/redigo/redis"
31- rg " github.com/redislabs/redisgraph-go"
29+ " fmt"
30+ " os"
31+
32+ " github.com/gomodule/redigo/redis"
33+ rg " github.com/redislabs/redisgraph-go"
3234)
3335
3436func main () {
35- conn , _ := redis.Dial (" tcp" , " 0.0.0.0:6379" )
36- defer conn.Close ()
37-
38- graph := rg.GraphNew (" social" , conn)
39-
40- john := rg.Node {
41- Label: " person" ,
42- Properties: map [string ]interface {}{
43- " name" : " John Doe" ,
44- " age" : 33 ,
45- " gender" : " male" ,
46- " status" : " single" ,
47- },
48- }
49- graph.AddNode (&john)
50-
51- japan := rg.Node {
52- Label: " country" ,
53- Properties: map [string ]interface {}{
54- " name" : " Japan" ,
55- },
56- }
57- graph.AddNode (&japan)
58-
59- edge := rg.Edge {
60- Source: &john,
61- Relation: " visited" ,
62- Destination: &japan,
63- }
64- graph.AddEdge (&edge)
65-
66- graph.Commit ()
67-
68- query := ` MATCH (p:person)-[v:visited]->(c:country)
37+ conn , _ := redis.Dial (" tcp" , " 127.0.0.1:6379" )
38+ defer conn.Close ()
39+
40+ graph := rg.GraphNew (" social" , conn)
41+
42+ graph.Delete ()
43+
44+ john := rg.Node {
45+ Label: " person" ,
46+ Properties: map [string ]interface {}{
47+ " name" : " John Doe" ,
48+ " age" : 33 ,
49+ " gender" : " male" ,
50+ " status" : " single" ,
51+ },
52+ }
53+ graph.AddNode (&john)
54+
55+ japan := rg.Node {
56+ Label: " country" ,
57+ Properties: map [string ]interface {}{
58+ " name" : " Japan" ,
59+ },
60+ }
61+ graph.AddNode (&japan)
62+
63+ edge := rg.Edge {
64+ Source: &john,
65+ Relation: " visited" ,
66+ Destination: &japan,
67+ }
68+ graph.AddEdge (&edge)
69+
70+ graph.Commit ()
71+
72+ query := ` MATCH (p:person)-[v:visited]->(c:country)
6973 RETURN p.name, p.age, c.name`
7074
71- // result is a QueryResult struct containing the query's generated records and statistics.
72- result , _ := graph.Query (query)
73-
74- // Pretty-print the full result set as a table.
75- result.PrettyPrint ()
76-
77- // Iterate over each individual Record in the result.
78- for result.Next () { // Next returns true until the iterator is depleted.
79- // Get the current Record.
80- r := result.Record ()
81-
82- // Entries in the Record can be accessed by index or key.
83- p_name := r.GetByIndex (0 )
84- fmt.Printf (" \n Name: %s \n " , p_name)
85- p_age , _ := r.Get (" p.age" )
86- fmt.Printf (" \n Age: %d \n " , p_age)
87- }
88-
89- // Path matching example.
90- query = " MATCH p = (:Person)-[:Visited]->(:Country) RETURN p"
91- result, _ = graph.Query (query)
92- res.Next ()
93- r := res.Record ()
94- p , ok := r.GetByIndex (0 ).(Path)
95- fmt.Printf (" %s " , p)
75+ // result is a QueryResult struct containing the query's generated records and statistics.
76+ result , _ := graph.Query (query)
77+
78+ // Pretty-print the full result set as a table.
79+ result.PrettyPrint ()
80+
81+ // Iterate over each individual Record in the result.
82+ fmt.Println (" Visited countries by person:" )
83+ for result.Next () { // Next returns true until the iterator is depleted.
84+ // Get the current Record.
85+ r := result.Record ()
86+
87+ // Entries in the Record can be accessed by index or key.
88+ pName := r.GetByIndex (0 )
89+ fmt.Printf (" \n Name: %s \n " , pName)
90+ pAge , _ := r.Get (" p.age" )
91+ fmt.Printf (" \n Age: %d \n " , pAge)
92+ }
93+
94+ // Path matching example.
95+ query = " MATCH p = (:person)-[:visited]->(:country) RETURN p"
96+ result , err := graph.Query (query)
97+ if err != nil {
98+ fmt.Println (err)
99+ os.Exit (1 )
100+ }
101+ fmt.Println (" Pathes of persons visiting countries:" )
102+ for result.Next () {
103+ r := result.Record ()
104+ p , ok := r.GetByIndex (0 ).(rg.Path )
105+ fmt.Printf (" %s %v \n " , p, ok)
106+ }
96107}
97108```
98109
0 commit comments