如果我在主要或func家庭中的任何地方使用chan,則該應用程序會運行,但實際上并不能正常工作。沒有引發任何錯誤,但是,它將不起作用。如果我刪除了頻道引用,它將恢復正常工作。通過在結構或全局通道中使用chan,應用程序將停止運行。在一個GET請求時,它返回h.Message從func home 由代碼添加任何頻道,GET請求不會返回消息。https://play.golang.org/p/-ZVcLhZRRRGpackage mainimport ( "fmt" "net/http" "github.com/gorilla/websocket" // _ "github.com/go-sql-driver/mysql")type WMessage struct { Message string `json:"message"` ch chan string}var upgrader = websocket.Upgrader{ ReadBufferSize: 1024, WriteBufferSize: 1024, CheckOrigin: func(r *http.Request) bool { return true },}var Chann chan stringfunc (h *WMessage) home(w http.ResponseWriter, r *http.Request) { h.Message = "hey this is the message from home" fmt.Fprintln(w, h.Message) fmt.Println(<-h.ch)}func main() { hom := &WMessage{} hom.ch = make(chan string) hom.ch <- "message sent" http.HandleFunc("/", hom.home) err := http.ListenAndServe(":3000", nil) if err != nil { fmt.Println("there was an error -> ", err) }}
- 1 回答
- 0 關注
- 327 瀏覽
添加回答
舉報
0/150
提交
取消