Skip to content
Hugo Jobling edited this page Oct 23, 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 fields 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