Couchbase with Golang
Written by Ryan Moore on 19 May 2014
Tweet
In this post I want to talk about using Couchbase with Go. To start off with we'll use the Couchbase driver on Github.
The couchbase driver is pretty easy to use. Just connect to the database, get your pool and bucket, and then start setting and getting data. The following code example assumes that you have couchbase installed and running locally as localhost
. For directions on how to install couchbase please reference the documentation on their website.
The code above doesn't do too many surprising things but there are some things that stood out to me when first using the Couchbase API. The first thing that I found interesting was the GetPool
method. If you read a lot of the Couchbase documentation Pools
are not really mentioned much, it generally appears as Cluster
. After doing some digging, it appears that the Couchbase team put the concept of a Pool into the codebase, but has not yet implemented features to support multiple pools. Currently you should always use the Default
pool.
The other thing that threw me off was in the Add
method. In the code above you'll notice the 0 in bucket.Add(user.Id, 0, user)
. That represents when that document should expire. Since we do not want the document to expire we pass in 0.
The Couchbase API is very easy to use in Go due to the automatic marshalling support that is built in. That's what all the `json:"name"`
stuff is about in the User
struct.
The code above and more can be found in this Github repo.
In the next post we'll explore using the Couchbase SDK in a Golang webapp.
comments powered by Disqus