A bucket is NOT CLOSED on the example of use

n1ql

#1

A bucket is NOT CLOSED on a example of use on Start Using the SDK.
https://developer.couchbase.com/documentation/server/current/sdk/go/start-using-sdk.html

defer bucket.Close() should be added after bucket, _ := cluster.OpenBucket("bucketname", "")

fixed:

package main

import (
	"fmt"
	"gopkg.in/couchbase/gocb.v1"
)

type User struct {
	Id string `json:"uid"`
	Email string `json:"email"`
	Interests []string `json:"interests"`
}

func main() {
        cluster, _ := gocb.Connect("couchbase://localhost")
        cluster.Authenticate(gocb.PasswordAuthenticator{
            Username: "USERNAME",
            Password: "PASSWORD",
        })
        bucket, _ := cluster.OpenBucket("bucketname", "")
        defer bucket.Close()
        
        bucket.Manager("", "").CreatePrimaryIndex("", true, false)
        
        bucket.Upsert("u:kingarthur",
                User{
                        Id: "kingarthur",
                        Email: "kingarthur@couchbase.com",
                        Interests: []string{"Holy Grail", "African Swallows"},
                }, 0)
        
        // Get the value back
        var inUser User
        bucket.Get("u:kingarthur", &inUser)
        fmt.Printf("User: %v\n", inUser)
        
        // Use query
        query := gocb.NewN1qlQuery("SELECT * FROM bucketname WHERE $1 IN interests")
        rows, _ := bucket.ExecuteN1qlQuery(query, []interface{}{"African Swallows"})
        var row interface{}
        for rows.Next(&row) {
                fmt.Printf("Row: %v", row)
        }
}

#2

Hey @usk,

I’ve notified our documentation team and they are on it! Thanks a lot for pointing this out.

Cheers, Brett