bucket.Get doesnt exist in go sdk v2.0.0 . is there a way to retrieve document by id?

hello,

i leveraged v2.0 for golang sdk and was able to leverage connect & bucket to make a connection & get the bucket info. however, i couldnt retrieve document by id and wondering if there is a way to get document by id

i have this so far

package main

import (
“fmt”

gocb "github.com/couchbase/gocb/v2"

)

func main() {

bucketName := “bucket”
opts := gocb.ClusterOptions{
	
	Username: “username”,
	Password: “password”,
	SecurityConfig: gocb.SecurityConfig{
		TLSSkipVerify: false,
	},
}
cluster, err := gocb.Connect("couchbase://host:port”, opts)
if err != nil {
	fmt.Println("Error in cluster connection", err)
}
bucket := cluster.Bucket(bucketName)

collection := bucket.DefaultCollection()

coldoc, cerr := collection.Get(“doc_id”, &gocb.GetOptions{})

if err != nil {
	fmt.Println("coldoc err", cerr)
}

fmt.Println("coldoc", coldoc)

}

i get document as nil . wondering if there is a way to retrieve doc details .

thanks
chakri

Hi @chakri_v, you’ve got the right idea. In your example after you do your Get you’re checking

if err != nil {
	fmt.Println("coldoc err", cerr)
}

I think that should be

if cerr != nil {
	fmt.Println("coldoc err", cerr)
}

I’m guessing that there must be an error retrieving the document. You can see how to access your data once you’ve got a successful result at https://docs.couchbase.com/go-sdk/2.0/howtos/kv-operations.html#retrieving-full-documents.

should have fixed my error handling…looks good thanks for sharing kv retrieval.

-chakri