That code seems to be correct (except that you might want a `if err != nil
{ break }` in there). I would say the reason you get empty messages is
because the other side is sending you empty messages.

On Sun, Feb 5, 2023 at 10:01 PM TH <tinsk...@gmail.com> wrote:

> Hey,
>
> I have a TCP client which reads data from server. Each message is ;
> delimited. I wonder what's the correct way to use bufio.Reader, as I'm
> getting empty messages.
>
> reader := bufio.NewReader(conn)
> for {
>     msg, err := reader.ReadBytes(59) // ; as delimiter
>     if err == nil {
>         fmt.Printf("%v\n", string(msg))
>     }
> }
>
> So sometimes I will get a msg that's only ";". I wonder if bufio.Reader
> can be re-used as I do it on my for loop? Or are there any handy packages
> that I could use to parse the ";" delimiter?
>
> I tried to write my own parser, but somehow bufio.Reader is like 3-4x
> faster even I thought I had an optimized code.
>
> Thanks
> - TH
>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/golang-nuts/e4c474c8-87b0-400d-9576-c26ad54f585en%40googlegroups.com
> <https://groups.google.com/d/msgid/golang-nuts/e4c474c8-87b0-400d-9576-c26ad54f585en%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAEkBMfF24y8OvEkD9%3DnkMAmFtE8Mqhhia4wY-9TYnxm4DqJfCQ%40mail.gmail.com.

Reply via email to