我在閱讀GOLANG-BOOK後開始玩golang。 我試圖建立一個簡單的TCP聊天。 我創建了一個用戶結構,並且我想要聽用戶數組中的每個user.inbound通道。編譯錯誤
我知道我的問題在於函數writeUser(),因爲它等待user.inbound。即時通訊不知道如何正確地使這種類型的用戶數組通道。
這是錯誤,我從編譯器收到:
./chatserver.go:22: syntax error: unexpected LCHAN, expecting)
./chatserver.go:25: non-declaration statement outside function body
./chatserver.go:31: non-declaration statement outside function body
./chatserver.go:32: syntax error: unexpected }
- 線22的FUNC writeUser()
這是我的代碼:
type User struct {
name string
inbound chan string
outbound chan string
conn net.Conn
}
func writeUser(user.inbound chan string) {
// how can I get the user connection?
err := gob.NewDecoder(user.conn).Encode(inbound)
if err != nil {
fmt.Println("Error: ", err)
}
}
func (chat *Chat) broadcast(username string, message string) {
outboundMessage := username + ": " + message;
for _, user := range chat.users {
user.inbound <- outboundMessage;
}
}
我編輯了你的問題標題 - 考慮到這裏沒有問題的頻道數組。 – 2014-11-04 21:32:44