我是个新手,我在玩这个notify码的套餐.
起初我有这样的代码:
func doit(w http.ResponseWriter, r *http.Request) {
notify.Post("my_event", "Hello World!")
fmt.Fprint(w, "+OK")
}
Hello World!doithandler
func handler(w http.ResponseWriter, r *http.Request) {
myEventChan := make(chan interface{})
notify.Start("my_event", myEventChan)
data := <-myEventChan
fmt.Fprint(w, data + "\n")
}
go run
$ go run lp.go
# command-line-arguments
./lp.go:15: invalid operation: data + "\n" (mismatched types interface {} and string)
在谷歌上搜索了一下,我找到了this question on SO个.
然后,我将代码更新为:
func handler(w http.ResponseWriter, r *http.Request) {
myEventChan := make(chan interface{})
notify.Start("my_event", myEventChan)
data := <-myEventChan
s:= data.(string) + "\n"
fmt.Fprint(w, s)
}
这是我应该做的吗?我的编译器错误消失了,所以我想这已经很不错了?这是有效的吗?你应该用不同的方式go 做吗?