【发布时间】:2012-12-26 16:33:33
【问题描述】:
我还是个新手,我正在玩这个 notify 包。
起初我的代码如下所示:
func doit(w http.ResponseWriter, r *http.Request) {
notify.Post("my_event", "Hello World!")
fmt.Fprint(w, "+OK")
}
我想将换行符附加到Hello World!,但不是在上面的函数doit 中,因为这很简单,但在之后的handler 中,如下所示:
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 type-mismatch