-1

Creating a TCP server that needs to process some data. I have a net.Conn instance "connection"from which I will read said data. The lower part of the snippet brings about an error noting that it cannot use the 'esc' value as a byte value.

const(
esc = "\a\n"
 )
....
c := bufio.NewReader(connection)
data, err := c.ReadBytes(esc)

Clearly, some conversion is needed but when I try

 const(
esc = "\a\n"
 )
....
c := bufio.NewReader(connection)
data, err := c.ReadBytes(byte(esc))

The compiler makes note that I cannot convert esc to byte. Is it due to the fact that I declared "\a\n" as a const value on the package level? Or is there something else overall associated with how I'm framing the bytes to be read?

Onlyartist9
  • 89
  • 1
  • 9
  • 1
    That "string of escape sequences" is just a string. The escape sequences are only in the source code, they don't exist in the in-memory representation. – Jonathan Hall Apr 19 '22 at 13:04

1 Answers1

1

You can't convert esc to byte because you can't convert strings into single bytes. You can convert a string into a byte slice ([]byte).

The bufio.Reader only supports single byte delimiters, you should use a bufio.Scanner with a custom split function instead for multi-byte delimiters.

Perhaps a modified version of https://stackoverflow.com/a/37531472/1205448

Dylan Reimerink
  • 5,874
  • 2
  • 15
  • 21