Go SDK 1.0

Go SDK 1.0

The Couchbase Go SDK allows you to connect to a Couchbase cluster from Go. It is a native Go library and uses the high-performance gocbcore to handle communicating to the cluster over Couchbase’s binary protocols.

Here's a sampling of what you can do with the Couchbase Go SDK:

Connecting

To connect to a bucket, just instantiate a new Cluster object to represent your cluster, and call the OpenBucket() method, passing in the name of the bucket to which you want to connect. The following example shows how to connect to a bucket on localhost:

myCluster, _ := gocb.Connect("couchbase://localhost")
myBucket, _ := myCluster.OpenBucket("default", "")

Operations

In most cases, operations are performed by invoking the relevant method while passing the key, any data and meta information that goes with it.

Here is an example of performing a get operation:

var value interface{}
cas, _ := myBucket.Get("document_name", &value)
fmt.Printf("Got value `%+v` with CAS `%08x`\n", value, cas)

Additionally, sometimes more information beyond the key is needed for an operation, here we perform an insert operation that takes a key, value, as well as an expiry time:

value := "test value"
cas, _ := myBucket.Insert("document_name", &value, 0)
fmt.Printf("Inserted document CAS is `%08x`\n", cas)

Performing Queries

In addition to the basic operations that are available, we additional provide methods to perform queries against your data. These queries allow you to ask questions like "What beers start with A?" rather than simply "Give me the beer that is called Aaas_Frankl." This is possible through the use of Couchbase's MapReduce views engine. The following is an example of performing a view query.

vq := gocb.NewViewQuery("beer", "by_name").Skip(6).Limit(3)
rows,err := myBucket.ExecuteViewQuery(vq)

var row interface{}
for rows.Next(&row) {
  fmt.Printf("Got row `%+v`\n")
}
if err := rows.Close(); err != nil {
  fmt.Printf("View query error: %s\n", err)
}