Skip to content
Matt Silverlock edited this page Oct 12, 2013 · 16 revisions
package main

import (
   r "github.com/dancannon/gorethink"
   "fmt"
)

var session *r.Session

func init(){
   var err error
   session, err = r.Connect(map[string]interface{}{
      "address":     "localhost:28015",
      "database":    "test",
   })  
   if err != nil {
      fmt.Println(err)
      return
   }   
}

// Struct tags are used to map struct fieldd to fields in the database
type Person struct {
   Id string       `gorethink:"id,omitempty"`
   Name string  `gorethink:"name"`
   Place string `gorethink:"place"`
}

func main(){
   // fetch all records from "persons" table
   rows,_ := r.Table("persons").Run(session)
   var persons []Person
   for rows.Next(){
      var p Person
      err := rows.Scan(&p)
      if err != nil {
         fmt.Println(err)
         return
      }   
      persons = append(persons, p)
   }   
   fmt.Println(persons)
}
Clone this wiki locally