For a simple golang chat/telnet client, I want to pass strings received from two bufio Readers to a select statement, so I can either send the user input to the server, or print the server sent data.
conn, _ := net.Dial("tcp", "localhost:8998")
for {
select{
case line, _ := bufio.NewReader(os.Stdin).ReadString('
'):
fmt.Print("> ")
fmt.Fprintf(conn, line + "
")
case data, _ := bufio.NewReader(conn).ReadString('
'):
fmt.Print(data)
}
}
The compiler gives me back this error
select case must be receive, send or assign recv
I suspect I should be using channels. But
conn, _ := net.Dial("tcp", "localhost:8998")
outgoing := make(chan string)
incoming := make(chan string)
for {
inputReader := bufio.NewReader(os.Stdin)
connReader := bufio.NewReader(conn)
o, _ := inputReader.ReadString('
')
i, _ := connReader.ReadString('
')
outgoing <- o
incoming <- i
select{
case out := <-outgoing:
fmt.Print("> ")
fmt.Fprintf(conn, out + "
")
case in := <-incoming:
fmt.Print(in)
}
}
But the code doesn't accept or receive data. Finally, I suspect I should be using two go routines to check for the Reader return value?