Skip to content

Instantly share code, notes, and snippets.

@chvck
Created Apr 8, 2022
Embed
What would you like to do?
package main
import (
"fmt"
"log"
"time"
"github.com/couchbase/gocb/v2"
)
func main() {
gocb.SetLogger(gocb.DefaultStdioLogger())
endpoint := "cb.c3juudskkglrhi2.cloud.couchbase.com"
bucketName := "default"
username := "dave"
password := "Pa$$w0rd"
// Initialize the Connection
cluster, err := gocb.Connect("couchbases://"+endpoint+"", gocb.ClusterOptions{
Authenticator: gocb.PasswordAuthenticator{
Username: username,
Password: password,
},
})
if err != nil {
log.Fatal(err)
}
_, err = cluster.Query("SELECT 1=1", nil)
if err != nil {
log.Fatal(err)
}
bucket := cluster.Bucket(bucketName)
err = bucket.WaitUntilReady(5*time.Second, nil)
if err != nil {
log.Fatal(err)
}
col := bucket.DefaultCollection()
_, err = col.Upsert("test", "inavalue", &gocb.UpsertOptions{})
if err != nil {
log.Fatal(err)
}
getResult, err := col.Get("test", nil)
if err != nil {
log.Fatal(err)
}
var val interface{}
err = getResult.Content(&val)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Val: %v\n", val)
cluster.Close(nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment