我正在尝试为 Go使用Faye 客户端,但是当我尝试为客户端创建接口时,由于回调中的类型,它似乎失败了:
package main
import (
"fmt"
"github.com/autogrowsystems/wray"
)
type Message interface {
Data() map[string]interface{}
}
type Promise interface {
Successful() bool
}
type Subscriber interface {
Subscribe(string, bool, func(Message)) (Promise, error)
}
func subscribeMe(subber Subscriber) {
subber.Subscribe("/my/chan", false, func(msg Message) {
fmt.Printf("got data: %+v", msg.Data())
})
}
func main() {
subscribeMe(wray.NewFayeClient("http://localhost/faye"))
fmt.Println("all good!")
}
此时它失败并出现以下错误:
# command-line-arguments
main/fayetest.go:27: cannot use wray.NewFayeClient("http://localhost/faye") (type *wray.FayeClient) as type Subscriber in argument to subscribeMe:
*wray.FayeClient does not implement Subscriber (wrong type for Subscribe method)
have Subscribe(string, bool, func(wray.Message)) (wray.SubscriptionPromise, error)
want Subscribe(string, bool, func(Message)) (Promise, error)
似乎它应该可以工作,因为客户端确实满足了接口:
// wray.go - wray.FayeClient
func (self *FayeClient) Subscribe(channel string, force bool, callback func(Message)) (promise SubscriptionPromise, err error) {
}
// response.go - wray.Message
func (self Message) Data() map[string]interface{} {
}
// wray.go - wray.SubscriptionPromise
func (self SubscriptionPromise) Successful() bool {
}
它应该可以正常工作吗?还是回调搞乱了界面的满意度?
编辑:顺便说一句,我是 Faye Client 那个分支的作者,我能做些什么来使它有利于接口?除了放弃回调?
萧十郎
噜噜哒
相关分类