Golang中使用Bolt访问值

huangapple go评论81阅读模式
英文:

Golang bolt access to value

问题

如何将john的值分配给value_variable?

英文:
var value_variable
        // Access data from within a read-only transactional block.
    db.View(func(tx *bolt.Tx) error {
        v := tx.Bucket([]byte("people")).Get([]byte("john"))
        fmt.Printf("John's last name is %s.\n", v)
        return nil
    })

How to assign john value to value_variable?

答案1

得分: 2

由于Go语言是词法作用域的,你可以在传递给View函数的函数内部分配value_variable的值:

var value_variable []byte

// 在只读事务块内访问数据。
db.View(func(tx *bolt.Tx) error {
    v := tx.Bucket([]byte("people")).Get([]byte("john"))
    value_variable = v // <----- 在这里分配值
    fmt.Printf("John's last name is %s.\n", v)
    return nil
})

请注意,这只是代码的翻译,我无法执行代码或提供其他功能。如果你有其他问题,请告诉我。

英文:

Since Go is lexically scoped, you can assign value_variable inside the function you pass into View:

var value_variable []byte

// Access data from within a read-only transactional block.
db.View(func(tx *bolt.Tx) error {
    v := tx.Bucket([]byte(&quot;people&quot;)).Get([]byte(&quot;john&quot;))
    value_variable = v // &lt;----- ASSIGN IT HERE
    fmt.Printf(&quot;John&#39;s last name is %s.\n&quot;, v)
    return nil
})

huangapple
  • 本文由 发表于 2015年3月18日 01:02:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/29105275.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定