我正在尝试创建包装Google Cloud Pubsub的pubsub.Client
和pubsub.Subscriber
的界面,以便在测试中模拟它们。为了使现有类型实现这些接口,我需要一些其方法来获取指针接收器。我该如何定义一个接口来做到这一点?
这是代码(文件简短,所以我包括了所有内容):
package cmd
import (
"context"
"cloud.google.com/go/pubsub"
)
type SubscriptionMaker interface {
Subscription(name string) (s Receiver)
}
// Pubsub clients implement SubscriptionMaker
var _ SubscriptionMaker = pubsub.Client{}
type Receiver interface {
Receive(context.Context, func(ctx context.Context, msg *pubsub.Message)) (err error)
}
// Pubsub subscriptions implement Receiver
var _ Receiver = pubsub.Subscription{}
这是我收到的错误消息:
go vet internal/cmd/common_types.go
# command-line-arguments
internal/cmd/common_types.go:14:5: cannot use "cloud.google.com/go/pubsub".Client literal (type "cloud.google.com/go/pubsub".Client) as type SubscriptionMaker in assignment:
"cloud.google.com/go/pubsub".Client does not implement SubscriptionMaker (wrong type for Subscription method)
have Subscription(string) *"cloud.google.com/go/pubsub".Subscription
want Subscription(string) Receiver
internal/cmd/common_types.go:21:5: cannot use "cloud.google.com/go/pubsub".Subscription literal (type "cloud.google.com/go/pubsub".Subscription) as type Receiver in assignment:
"cloud.google.com/go/pubsub".Subscription does not implement Receiver (Receive method has pointer receiver)