我是一个完整的围棋新手,很抱歉提前提出问题。
我正在尝试使用如此定义的接口来连接到消息代理:
// Broker is an interface used for asynchronous messaging.
type Broker interface {
Options() Options
Address() string
Connect() error
Disconnect() error
Init(...Option) error
Publish(string, *Message, ...PublishOption) error
Subscribe(string, Handler, ...SubscribeOption) (Subscriber, error)
String() string
}
// Handler is used to process messages via a subscription of a topic.
// The handler is passed a publication interface which contains the
// message and optional Ack method to acknowledge receipt of the message.
type Handler func(Publication) error
// Publication is given to a subscription handler for processing
type Publication interface {
Topic() string
Message() *Message
Ack() error
}
我正在尝试使用Subscribe
-function 订阅一个频道,这就是我现在苦苦挣扎的地方。我目前的方法是以下一种:
natsBroker.Subscribe(
"QueueName",
func(p broker.Publication) {
fmt.Printf(p.Message)
},
)
错误输出是cannot use func literal (type func(broker.Publication)) as type broker.Handler in argument to natsBroker.Subscribe
。
但是我如何确保函数类型实际上是 a broker.Handler
?
谢谢你的时间!
更新
万一有人感兴趣,错误返回类型丢失导致错误,所以它应该看起来类似于:
natsBroker.Subscribe("QueueName", broker.Handler(func(p broker.Publication) error { fmt.Printf(p.Topic()) return nil }), )