英文:
Compiler errors
问题
我已经开始阅读《GOLANG-BOOK》后开始使用golang,并尝试构建一个简单的TCP聊天程序。我已经创建了一个用户结构体,并且我想监听来自用户数组中每个用户的user.inbound通道。
我知道我在这里的问题是在writeUser()函数中,因为它正在等待user.inbound。我不确定如何正确地将这种通道化与用户数组结合起来。
这是编译器给出的错误:
./chatserver.go:22: 语法错误: 意外的LCHAN,期望)
./chatserver.go:25: 非声明语句在函数体外
./chatserver.go:31: 非声明语句在函数体外
./chatserver.go:32: 语法错误: 意外的}
- 第22行是func writeUser()
这是我的代码:
type User struct {
name string
inbound chan string
outbound chan string
conn net.Conn
}
func writeUser(user.inbound chan string) {
// 我如何获取用户连接?
err := gob.NewDecoder(user.conn).Encode(inbound)
if err != nil {
fmt.Println("错误: ", err)
}
}
func (chat *Chat) broadcast(username string, message string) {
outboundMessage := username + ": " + message;
for _, user := range chat.users {
user.inbound <- outboundMessage;
}
}
英文:
I've started playing with golang after reading the GOLANG-BOOK.
and I'm trying to build a simple TCP chat.
I've created a user struct, and I want to listen to every user.inbound channel from the users array.
I know my problem here is with the function writeUser() since its waiting for user.inbound. im not sure how to properly make this kind channeling with array of users.
this is the errors I recieve from the compiler:
./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 }
- line 22 is the func writeUser()
And this is my Code:
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;
}
}
答案1
得分: 2
你在函数签名中尝试导航到用户的inbound
通道:
func recieve(user.inbound chan string) {
// ^^^^^ - 这部分是错误的
在函数参数中不能像这样导航到对象的属性 - 这实际上没有意义。在这个上下文中,你想要的是_任何字符串通道_:
func recieve(inbound chan string) {
你要如何处理这个通道,我会留给你决定(无论如何,你似乎在函数中没有对它做任何操作)。
其他错误无疑是从第一个错误中堆积而来的 - 因为这样的错误可能会使解析器出错。
此外,在你的broadcast
方法的底部有一些随机悬挂的分号。
英文:
You've tried to navigate to the inbound
channel on the user in your function signature:
func recieve (user.inbound chan string) {
// ^^^^^ - that part is wrong
You can't navigate to an objects property like that in a function argument - it doesn't really make sense. What you want in this context, is any channel of strings:
func recieve (inbound chan string) {
Exactly what you'll do with that channel I will leave to you (you don't seem to do anything with it in the function anyway).
The other errors are no doubt stacked from the first one - given that such an error can throw the parser out of whack.
Also, you have a few random dangling semicolons down the bottom in your broadcast
method.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论