I am trying to read from a network connection, in the following way:
func getIn(conn net.Conn){
for{
in, err := bufio.NewReader(conn).ReadString('
')
if err!=nil{
fmt.Printf(err.Error())
}
fmt.Printf("[%s]", in)
}
}
The stream of input being sent to that connection is of the following pattern:
message1 message2 message3 message4 message5
etc...
I noticed that my function skips messages, outputting, for instance,:
message1 message2 message4 message5
This leads me to think that the bufio ReadString method discards the incoming buffer every time a newline character is encountered. Say the buffer consists of:
message1 mess
at the moment of reading. Then, message1 gets read and the remaining part mess is discarded. This does not entirely make sense either, because then the next input should be age2, but in reality it is message3.
I used a different function, net.Conn.Read(), which indeed does not skip any part of the input, but requires more string parsing on my side. How can I make the ReadString() function work for me?