编译器错误

huangapple go评论92阅读模式
英文:

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(&quot;Error: &quot;, err)
    }
}


func (chat *Chat) broadcast(username string, message string) {
	outboundMessage := username + &quot;: &quot; + message;

	for _, user := range chat.users {
		user.inbound &lt;- 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.

huangapple
  • 本文由 发表于 2014年11月5日 05:23:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/26745304.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定