Compiler errors

Issue

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;
    }
}

Solution

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.

Answered By – Simon Whitehead

Answer Checked By – Marie Seifert (GoLangFix Admin)

Leave a Reply

Your email address will not be published.