Skip to content

Instantly share code, notes, and snippets.

@chritchens
Last active December 21, 2015 01:58
Show Gist options
  • Save chritchens/6231425 to your computer and use it in GitHub Desktop.
Save chritchens/6231425 to your computer and use it in GitHub Desktop.
An example of using go-couchbase, a Go driver for Couchbase, for views.
package main
import "fmt"
import "log"
import "github.com/couchbaselabs/go-couchbase"
/* the view is a JavaScript map function that returns a list of key-value rows. Each row has a birthday as key and the document id as value. To obtain the entire doc, just replace `meta.id` with `doc`.
function (doc, meta) {
if(doc.birthday) {
emit(doc.birthday, meta.id);
}
}
*/
func main() {
people, err := couchbase.GetBucket("http://localhost:8091/", "default", "people")
if err != nil {
log.Fatalf("Failed to connect to the bucket people:", err)
}
res, err := people.View("dev_people", "by_age", map[string]interface{}{
// a hack: the compiler compels to add a comma before a newline
// I prefer to have a newline because it make the code more readable
"descending": true,
})
if err != nil {
log.Fatalf("Failed to get the view by_age:", err)
}
fmt.Println("Total rows: ", res.TotalRows)
fmt.Println("Rows:", res.Rows)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment