Reusing Collections or buckets

Should I reuse collections or bucket? Can I pass pointer to collections in a function or methods to execute some query. It throws me an error that the collections type is undefined

Hi @rkunhi yes you should create a bucket/collection once and then pass it around to your functions. Can you post the error that you’re seeing and some code that reproduces the issue?

I am a beginner in Go lang. :slight_smile: I passed the collection type to a function. it shows error - undefined Collection
…///connect to cluster
//use connection to get a bucket
//Get a pointer to collection
collection := bucket.DefaultCollection()
getData(collection)
I want to pass this collection pointer to a function
///Pass collection to a function …displays error - Undefined Collection
func getData( c *Collection) {
}
Or should I create global variables for collection bucket and cluster

Hi @rkunhi I think that I see the problem. func getData( c *Collection) { here, Collection isn’t defined within your project. If you try func getData( c *gocb.Collection) { then it’ll tell it that Collection belongs to the gocb package.

You can find a load of examples for using the Go SDK at https://github.com/couchbaselabs/sdk-examples/tree/master/go and https://github.com/couchbaselabs/sdk-examples/blob/master/go/using-cas.go specifically has examples of using Collection as a function parameter. To run any of the examples then they need to be run individually rather than built as a whole project - e.g. go run using-cas.go

What a timing? I figured out my silly mistake and at the same time I receive your quick response. Thank you very much. And thanks for sharing the examples they are good.