Go language driver for RethinkDB made by Christopher Hesse
Current supported RethinkDB version: 1.7.1
go get -u github.com/christopherhesse/rethinkgo
If you do not have the goprotobuf runtime installed, it is required:
brew install mercurial # if you do not have mercurial installed
go get code.google.com/p/goprotobuf/{proto,protoc-gen-go}
package main
import (
"fmt"
r "github.com/christopherhesse/rethinkgo"
)
type Employee struct {
FirstName string
LastName string
Job string
Id string `json:"id,omitempty"` // (will appear in json as "id", and not be sent if empty)
}
func main() {
// To access a RethinkDB database, you connect to it with the Connect function
session, err := r.Connect("localhost:28015", "company_info")
if err != nil {
fmt.Println("error connecting:", err)
return
}
var response []Employee
// Using .All(), we can read the entire response into a slice, without iteration
err = r.Table("employees").Run(session).All(&response)
if err != nil {
fmt.Println("err:", err)
} else {
fmt.Println("response:", response)
}
// If we want to iterate over each result individually, we can use the rows
// object as an iterator
rows := r.Table("employees").Run(session)
for rows.Next() {
var row Employee
if err = rows.Scan(&row); err != nil {
fmt.Println("err:", err)
break
}
fmt.Println("row:", row)
}
if err = rows.Err(); err != nil {
fmt.Println("err:", err)
}
}
The Go driver is most similar to the official Javascript driver.
Most of the functions have the same names as in the Javascript driver, only with the first letter capitalized. See Go Driver Documentation for examples and documentation for each function.
To use RethinkDB with this driver, you build a query using r.Table(), for example, and then call query.Run(session) to execute the query on the server and return an iterator for the results.
There are 3 convenience functions on the iterator if you don't want to iterate over the results, .One(&result) for a query that returns a single result, .All(&results) for multiple results, and .Exec() for a query that returns an empty response (for instance, r.TableCreate(string)) or to ignore the response.
The important types are r.Exp (for RethinkDB expressions), r.Query (interface for all queries, including expressions), r.List (used for Arrays, an alias for []interface{}), and r.Map (used for Objects, an alias for map[string]interface{}).
The function r.Expr() can take arbitrary structs and uses the "json" module to serialize them. This means that structs can use the json.Marshaler interface (define a method MarshalJSON() on the struct). Also, struct fields can also be annotated to specify their JSON equivalents:
type MyStruct struct {
MyField int `json:"my_field"` // (will appear in json as my_field)
OtherField int // (will appear in json as OtherField)
}
See the json docs for more information.
Changes for RethinkDB 1.7.1:
Changes for RethinkDB 1.6.1:
dest
, here's when you should use the different methods:
There's no r(attributeName) or row[attributeName] function call / item indexing to get attributes of the "current" row or a specific row respectively. Instead, there is a .Attr() method on the global "Row" object (r.Row) and any row Expressions that can be used to access attributes. Examples:
r.Table("marvel").OuterJoin(r.Table("dc"),
func(marvel, dc r.Exp) interface{} {
return marvel.Attr("strength").Eq(dc.Attr("strength"))
})
r.Table("marvel").Map(r.Row.Attr("strength").Mul(2))