在 Go 中包装一个指针
Wrapping a pointer in Go
库 foo
在该库 returns 中公开了一个类型 A
和一个函数 Fn
*A
.
我为 A
定义了一个名为 B
:
的 "wrapper"
type B foo.A
我可以在不取消引用 A
的情况下将 *A
转换为 *B
吗?
也就是说,如果我有
a := foo.Fn() // a is a *A
b := B(*a)
return &b
如何在不使用 *a
的情况下将 *a
转换为 *b
?
我问的原因是在我使用的库中,github.com/coreos/bbolt
,从 Open
函数返回的 *DB
值包括一个 sync.Mutex
和所以当我尝试复制 Mutex
.
时编译器会抱怨
更新以解释我将如何使用它
我有一个
type Datastore struct {
*bolt.DB
}
我也有一个像这样的功能(其中之一):
func (ds *Datastore) ReadOne(bucket, id string, data interface{}) error {
return ds.View(func(tx *bolt.Tx) error {
b, err := tx.CreateBucketIfNotExists([]byte(bucket))
if err != nil {
return fmt.Errorf("opening bucket %s: %v", bucket, err)
}
bytes := b.Get([]byte(id))
if bytes == nil {
return fmt.Errorf("id %s not found", id)
}
if err := json.Unmarshal(bytes, data); err != nil {
return fmt.Errorf("unmarshalling item: %v", err)
}
return nil
})
}
我想使用哈希映射模拟底层的 BoltDB 数据库。我 运行 遇到了一个问题,因为 View
需要一个接受 bolt.Tx
的函数。然后 tx
用于在 CreateBucketIfNotExists
中创建一个新桶。我不能用调用我的散列映射模拟版本 CreateBucketIfNotExists
.
的匿名函数参数替换该匿名函数参数
我想到了这个:
package boltdb
import (
"github.com/coreos/bbolt"
)
type (
bucket bolt.Bucket
// Bucket is a wrapper for bolt.Bucket to facilitate mocking.
Bucket interface {
ForEach(fn func([]byte, []byte) error) error
Get(key []byte) []byte
NextSequence() (uint64, error)
Put(key, value []byte) error
}
db bolt.DB
// DB is a wrapper for bolt.DB to facilitate mocking.
DB interface {
Close() error
Update(fn func(*Tx) error) error
View(fn func(*Tx) error) error
}
transaction bolt.Tx
// Tx is a wrapper for bolt.Tx to facilitate mocking.
Tx interface {
CreateBucketIfNotExists(name []byte) (Bucket, error)
}
)
// ForEach executes a function for each key/value pair in a bucket.
func (b *bucket) ForEach(fn func([]byte, []byte) error) error {
return ((*bolt.Bucket)(b)).ForEach(fn)
}
// Get retrieves the value for a key in the bucket.
func (b *bucket) Get(key []byte) []byte {
return ((*bolt.Bucket)(b)).Get(key)
}
// NextSequence returns an autoincrementing integer for the bucket.
func (b *bucket) NextSequence() (uint64, error) {
return ((*bolt.Bucket)(b)).NextSequence()
}
// Put sets the value for a key in the bucket.
func (b *bucket) Put(key, value []byte) error {
return ((*bolt.Bucket)(b)).Put(key, value)
}
// Close releases all database resources.
func (db *db) Close() error {
return ((*bolt.DB)(db)).Close()
}
// Update executes a function within the context of a read-write managed transaction.
func (db *db) Update(fn func(Tx) error) error {
return ((*bolt.DB)(db)).Update(func(tx *bolt.Tx) error {
t := transaction(*tx)
return fn(&t)
})
}
// View executes a function within the context of a managed read-only transaction.
func (db *db) View(fn func(Tx) error) error {
return ((*bolt.DB)(db)).View(func(tx *bolt.Tx) error {
t := transaction(*tx)
return fn(&t)
})
}
// CreateBucketIfNotExists creates a new bucket if it doesn't already exist.
func (tx *transaction) CreateBucketIfNotExists(name []byte) (Bucket, error) {
b, err := ((*bolt.Tx)(tx)).CreateBucketIfNotExists(name)
if err != nil {
return nil, err
}
w := bucket(*b)
return &w, nil
}
到目前为止,在我的代码中,我只使用了上面显示的函数。如果新代码需要,我可以添加更多内容。
我将在实际代码中将每个 bolt.DB
替换为 DB
,将 bolt.Tx
替换为 Tx
,将 bolt.Bucket
替换为 Bucket
。模拟器将对使用底层哈希映射而不是存储到磁盘的所有三种类型使用替换。然后我可以测试我的所有代码,一直到数据库调用。
您可以简单/直接将类型*A
的值转换为类型*B
的值,您只需要括号*B
:
a := foo.Fn() // a is a *A
b := (*B)(a)
return b
你甚至可以转换函数调用的return值:
return (*B)(foo.Fn())
在 Go Playground 上试试。
这是可能的,因为Spec: Conversions:
A non-constant value x
can be converted to type T
in any of these cases:
x
is assignable to T
.
...
A value x
is assignable to a variable of type T
("x
is assignable to T
") if one of the following conditions applies:
...
x
's type V
and T
have identical underlying types and at least one of V
or T
is not a defined type.
*B
和*A
类型都没有定义,*B
的底层类型和*A
的底层类型相同(都是指针A
).
的类型声明中的任何类型的基础类型
库 foo
在该库 returns 中公开了一个类型 A
和一个函数 Fn
*A
.
我为 A
定义了一个名为 B
:
type B foo.A
我可以在不取消引用 A
的情况下将 *A
转换为 *B
吗?
也就是说,如果我有
a := foo.Fn() // a is a *A
b := B(*a)
return &b
如何在不使用 *a
的情况下将 *a
转换为 *b
?
我问的原因是在我使用的库中,github.com/coreos/bbolt
,从 Open
函数返回的 *DB
值包括一个 sync.Mutex
和所以当我尝试复制 Mutex
.
更新以解释我将如何使用它
我有一个
type Datastore struct {
*bolt.DB
}
我也有一个像这样的功能(其中之一):
func (ds *Datastore) ReadOne(bucket, id string, data interface{}) error {
return ds.View(func(tx *bolt.Tx) error {
b, err := tx.CreateBucketIfNotExists([]byte(bucket))
if err != nil {
return fmt.Errorf("opening bucket %s: %v", bucket, err)
}
bytes := b.Get([]byte(id))
if bytes == nil {
return fmt.Errorf("id %s not found", id)
}
if err := json.Unmarshal(bytes, data); err != nil {
return fmt.Errorf("unmarshalling item: %v", err)
}
return nil
})
}
我想使用哈希映射模拟底层的 BoltDB 数据库。我 运行 遇到了一个问题,因为 View
需要一个接受 bolt.Tx
的函数。然后 tx
用于在 CreateBucketIfNotExists
中创建一个新桶。我不能用调用我的散列映射模拟版本 CreateBucketIfNotExists
.
我想到了这个:
package boltdb
import (
"github.com/coreos/bbolt"
)
type (
bucket bolt.Bucket
// Bucket is a wrapper for bolt.Bucket to facilitate mocking.
Bucket interface {
ForEach(fn func([]byte, []byte) error) error
Get(key []byte) []byte
NextSequence() (uint64, error)
Put(key, value []byte) error
}
db bolt.DB
// DB is a wrapper for bolt.DB to facilitate mocking.
DB interface {
Close() error
Update(fn func(*Tx) error) error
View(fn func(*Tx) error) error
}
transaction bolt.Tx
// Tx is a wrapper for bolt.Tx to facilitate mocking.
Tx interface {
CreateBucketIfNotExists(name []byte) (Bucket, error)
}
)
// ForEach executes a function for each key/value pair in a bucket.
func (b *bucket) ForEach(fn func([]byte, []byte) error) error {
return ((*bolt.Bucket)(b)).ForEach(fn)
}
// Get retrieves the value for a key in the bucket.
func (b *bucket) Get(key []byte) []byte {
return ((*bolt.Bucket)(b)).Get(key)
}
// NextSequence returns an autoincrementing integer for the bucket.
func (b *bucket) NextSequence() (uint64, error) {
return ((*bolt.Bucket)(b)).NextSequence()
}
// Put sets the value for a key in the bucket.
func (b *bucket) Put(key, value []byte) error {
return ((*bolt.Bucket)(b)).Put(key, value)
}
// Close releases all database resources.
func (db *db) Close() error {
return ((*bolt.DB)(db)).Close()
}
// Update executes a function within the context of a read-write managed transaction.
func (db *db) Update(fn func(Tx) error) error {
return ((*bolt.DB)(db)).Update(func(tx *bolt.Tx) error {
t := transaction(*tx)
return fn(&t)
})
}
// View executes a function within the context of a managed read-only transaction.
func (db *db) View(fn func(Tx) error) error {
return ((*bolt.DB)(db)).View(func(tx *bolt.Tx) error {
t := transaction(*tx)
return fn(&t)
})
}
// CreateBucketIfNotExists creates a new bucket if it doesn't already exist.
func (tx *transaction) CreateBucketIfNotExists(name []byte) (Bucket, error) {
b, err := ((*bolt.Tx)(tx)).CreateBucketIfNotExists(name)
if err != nil {
return nil, err
}
w := bucket(*b)
return &w, nil
}
到目前为止,在我的代码中,我只使用了上面显示的函数。如果新代码需要,我可以添加更多内容。
我将在实际代码中将每个 bolt.DB
替换为 DB
,将 bolt.Tx
替换为 Tx
,将 bolt.Bucket
替换为 Bucket
。模拟器将对使用底层哈希映射而不是存储到磁盘的所有三种类型使用替换。然后我可以测试我的所有代码,一直到数据库调用。
您可以简单/直接将类型*A
的值转换为类型*B
的值,您只需要括号*B
:
a := foo.Fn() // a is a *A
b := (*B)(a)
return b
你甚至可以转换函数调用的return值:
return (*B)(foo.Fn())
在 Go Playground 上试试。
这是可能的,因为Spec: Conversions:
A non-constant value
x
can be converted to typeT
in any of these cases:
x
is assignable toT
....
A value
x
is assignable to a variable of typeT
("x
is assignable toT
") if one of the following conditions applies:
...
x
's typeV
andT
have identical underlying types and at least one ofV
orT
is not a defined type.
*B
和*A
类型都没有定义,*B
的底层类型和*A
的底层类型相同(都是指针A
).