gocb:批量获取操作示例

最近发布的 gocb lib(couchbase 的官方 golang 客户端)提供了用于执行批量操作(获取、删除等)的 API。我很想看到这样一个操作的完整示例,唉——我的围棋技能缺乏,网上也没有。


我想看到一个片段,(1) 编译和 (2) 执行多重获取,最后 (3) 设法访问从 couchbase 返回的值。


这是在线存在的小文档:http : //developer.couchbase.com/documentation/server/4.0/sdks/go-beta/bulk-operations.html


以下代码(执行插入)是不够的:我想要 Get(特别是 - 如何查看 get 操作结果的内容)。


myBucket, _ := myCluster.OpenBucket("default", "")


var items []gocb.BulkOp

items = append(items, &gocb.InsertOp{Key: "document_name_1", Value: "Hello World 1"})

items = append(items, &gocb.InsertOp{Key: "document_name_2", Value: "Hello World 2"})


err := bucket.Do(items)


牛魔王的故事
浏览 212回答 1
1回答

四季花海

只需用*InsertOp值替换*GetOp值,然后读取它们的Value字段:package mainimport "fmt"import "gopkg.in/couchbaselabs/gocb.v1"func main() {&nbsp; &nbsp; &nbsp; &nbsp; getKeys()}func getKeys() {&nbsp; &nbsp; &nbsp; &nbsp; myCluster, err := gocb.Connect("couchbase://<couchbase-address>")&nbsp; &nbsp; &nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("cluster error:", err)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; myBucket, err := myCluster.OpenBucket("Test", "") //case sensitive!&nbsp; &nbsp; &nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("bucket error:", err)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; var items []gocb.BulkOp&nbsp; &nbsp; &nbsp; &nbsp; items = append(items, &gocb.GetOp{Key: "document_name_1"})&nbsp; &nbsp; &nbsp; &nbsp; items = append(items, &gocb.GetOp{Key: "document_name_2"})&nbsp; &nbsp; &nbsp; &nbsp; err = myBucket.Do(items)&nbsp; &nbsp; &nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("Do error:", err)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; panic(err)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; for _, g := range items {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //"downcast" the instance back to its concrete type - GetOp&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; t := g.(*gocb.GetOp)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(t)&nbsp; &nbsp; &nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go