File README.SUSE of Package golang-googlecode-couch-go.6992
Basic usage
First, create a Database object to represent the DB you'll be interacting with
db, err := couch.NewDatabase("127.0.0.1", "5984", "databasename")
A CouchDB document is represented by a Go struct
type Record struct {
Type string
Count uint64
Elements []string
MyMap map[string]string
}
You can Insert these documents directly
r := Record{...}
id, rev, err := db.Insert(r)
and Retrieve them by ID.
r := new(Record)
rev, err := db.Retrieve(id, r)
If you want to specify document IDs, rather than have them auto-generated, you can use the InsertWith function
r := Record{...}
_, rev, err := db.InsertWith(r, "record_001")
or embed the _id in your document structure
type CompleteRecord struct {
Id string "_id"
Rev string "_rev" // useful only for Retrieve and Edit
Foo string
Count uint64
}
r := CompleteRecord{Id:"id001", Rev:"", Foo:"abc", Count:0}
_, rev, err := db.Insert(r)
You can also Edit a document, either by manually specifying the id and current revision
r := new(Record)
current_rev, err := db.Retrieve(some_id, r)
r.Count = r.Count + 1
current_rev, err = db.EditWith(r, some_id, current_rev)
if err != nil {
// edit failed, out of sync?
}
or, as with Insert, by embedding _id and _rev fields in the document structure
complete_r = new(CompleteRecord) // has Id and Rev
current_rev, err := db.Retrieve(some_id, complete_r)
complete_r.Count = complete_r.Count + 1
current_rev, err = db.Edit(complete_r)
if err != nil {
// edit failed, out of sync?
}
Delete works as expected
if err := db.Delete(id, current_rev); err != nil {
// delete failed, out of sync?
}
You can also perform simple queries on views
func (p Database) Query(view string, options map[string]interface{}) ([]string, os.Error)
Check the source for more details.
Gotchas
couch-go uses the reflect package (by way of the json package) to read and write documents via the CouchDB API. reflect requires that struct fields are public (ie. begin with a capital letter) to read from and write to them. So, your document structures in Go must contain capitalized field names.
The behavior of the Insert and Edit classes of functions depend on the contents of the struct you pass to them