Golang bolt访问价值

时间:2015-03-17 17:02:41

标签: go

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
    })

如何将john值赋给value_variable?

1 个答案:

答案 0 :(得分:1)

由于Go为lexically scoped,您可以在传递给value_variable的函数中指定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("people")).Get([]byte("john"))
    value_variable = v // <----- ASSIGN IT HERE
    fmt.Printf("John's last name is %s.\n", v)
    return nil
})