Sono abbastanza nuovo per andare e stavo giocando con questa notifica pacchetto di .
All'inizio avevo un codice simile al seguente:
func doit(w http.ResponseWriter, r *http.Request) {
notify.Post("my_event", "Hello World!")
fmt.Fprint(w, "+OK")
}
Volevo aggiungere newline a Hello World!
ma non nella funzione doit
sopra, perché sarebbe piuttosto banale, ma in handler
seguito come questo sotto:
func handler(w http.ResponseWriter, r *http.Request) {
myEventChan := make(chan interface{})
notify.Start("my_event", myEventChan)
data := <-myEventChan
fmt.Fprint(w, data + "\n")
}
Dopo go run
:
$ go run lp.go
# command-line-arguments
./lp.go:15: invalid operation: data + "\n" (mismatched types interface {} and string)
Dopo un po 'di Google ho trovato questa domanda su SO .
Quindi ho aggiornato il mio codice per:
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)
}
È questo che dovevo fare? I miei errori del compilatore sono spariti, quindi immagino che sia abbastanza buono? È efficiente? Dovresti farlo diversamente?