मैं जाने के लिए बहुत नया हूं और मैं इस अधिसूचित पैकेज के साथ खेल रहा था ।
पहले मेरे पास कोड था जो इस तरह दिखता था:
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)
थोड़ा सा Googling के बाद मुझे 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)
}
क्या यह मुझे करना चाहिए था? मेरी संकलक त्रुटियां दूर हो गई हैं इसलिए मुझे लगता है कि यह बहुत अच्छा है? क्या यह कुशल है? क्या आपको इसे अलग तरह से करना चाहिए?