【发布时间】:2019-04-17 20:01:36
【问题描述】:
我是一个完整的围棋新手,很抱歉提前提出问题。
我正在尝试使用这样定义的接口来连接到消息代理:
// 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。
但是如何确保函数类型实际上是broker.Handler?
提前感谢您的时间!
更新
如果有人感兴趣,错误返回类型丢失导致错误,所以它应该看起来类似于:
natsBroker.Subscribe( "队列名", broker.Handler(func(p broker.Publication)错误{ fmt.Printf(p.Topic()) 返回零 }), )
【问题讨论】: