Created
March 21, 2015 00:04
-
-
Save dancannon/2865686d163ed78bbc3c to your computer and use it in GitHub Desktop.
New channel based iterator in GoRethink v1.0
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
cursor, err := r.Expr([]int{1,2,3}).Run(session) | |
if err != nil { | |
panic(err) | |
} | |
ch := make(chan int) | |
cursor.Listen(ch) | |
<- ch // 1 | |
<- ch // 2 | |
<- ch // 3 |
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 ( | |
"log" | |
"time" | |
r "github.com/dancannon/gorethink" | |
) | |
const address = "localhost:28015" | |
type doc struct { | |
Id string `gorethink:"name"` | |
ParentId string `gorethink:"parent_id"` | |
Username string `gorethink:"username"` | |
Status int `gorethink:"status"` | |
Time time.Time `gorethink:"time"` | |
} | |
type docChange struct { | |
NewVal doc `gorethink:"new_val"` | |
OldVal doc `gorethink:"old_val"` | |
} | |
func main() { | |
s, err := r.Connect(r.ConnectOpts{ | |
Address: address, | |
}) | |
s.SetMaxIdleConns(10) | |
s.SetMaxOpenConns(100) | |
if err != nil { | |
panic(err) | |
} | |
defer s.Close() | |
log.Println("Connected") | |
cursor, err := r.Table("docs").Changes().Run(s) | |
if err != nil { | |
panic(err) | |
} | |
ch := make(chan docChange) | |
cursor.Listen(ch) | |
log.Println("Listening for changes") | |
for change := range ch { | |
log.Printf("Change %#v", change) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment